php trait使用另一个特性

 昵称这种东西真的好难取好吗 发布于 2023-02-08 14:29

我有一个使用另一个特征的特征,现在我收到了关于类中不存在的函数的错误.我简化了代码:

settings.php配置:

getMessage());}
    }
}
?>

为database.php

pdo=new PDO("mysql:host=".$this->getSetting("db","host").";dbname=".$this->getSetting("db","database"),$this->getSetting("db","user"),$this->getSetting("db","password"));
            $this->init();
        }
        catch(PDOException $e){throw new Exception($e->getMessage());}
    }
}
?>

users.php

connect();
        }
        catch(Exception $e){throw new Exception($e->getMessage());}
    }
    public function __destruct(){
        unset($this);
    }
    public function isAdmin(){
        try{
            if($this->loginStatus()===true){

            }
            else return false;
        }
        catch(Exception $e){throw new Exception($e->getMessage());}
    }
    public function loginStatus(){
        if(!$this->getSession("tysus")||!$this->getSession("tyspw"))return false;// user is not logged in because we couldn't find session with username and/or password
        if(!$this->userExists($this->getSession("tysus"),$this->getSession("tyspw")))return false;// user is unknown to database
        return true;// other checks failed, user must be logged in
    }
}
?>

现在我收到了这个错误:

致命错误:在第18行的/home/deb2371/domains/nonamenohistory.com/public_html/include/classes/class.database.php中调用未定义的方法users :: readSetting()

我认为会发生这样的事情:类用户使用特质数据库,特质数据库将使用特征设置和特征会话.

如果是这种情况,我不会得到任何错误,但遗憾的是情况并非如此.

有人知道如何解决这个问题吗?

2 个回答
  • 也许是因为readSetting实际上叫做getSetting?

    2023-02-08 14:31 回答
  • 代码重用是面向对象编程最重要的方面之一.

    一个简单的例子Multiple Traits,并Composing Multiple Traits通过它可以很容易地分析你的情况.

      使用多种特质

    一个类可以使用多个特征.以下示例演示如何在IDE类中使用多个特征.它为了演示而模拟PHP中的C编译模型.

    <?php
    
     trait Preprocessor{
     function preprocess() {
        echo 'Preprocess...done'. '<br/>';
      }
    }
    trait Compiler{
    function compile() {
       echo 'Compile code... done'. '<br/>';
      }
    }
    
    trait Assembler{
    function createObjCode() {
       echo 'Create the object code files... done.' . '<br/>';
     }
    }
    
    trait Linker{
    function createExec(){
       echo 'Create the executable file...done' . '<br/>';
      }
    }
    
    class IDE{
    use Preprocessor, Compiler, Assembler, Linker;
    
    function run() {
     $this->preprocess();
     $this->compile();
     $this->createObjCode();
     $this->createExec();
    
      echo 'Execute the file...done' . '<br/>';
     }
    }
    $ide = new IDE();
    $ide->run();
    

      组成多个特征

    通过在特征声明中使用use语句,特征可以由其他特征组成.请参阅以下示例:

    <?php
    
    trait Reader{
    public function read($source){
       echo sprintf("Read from %s <br/>",$source);
      }
    }
    
    trait Writer{
    public function write($destination){
       echo sprintf("Write to %s <br/>",$destination);
      }
    }
    
    trait Copier{
    use Reader, Writer;
    public function copy($source,$destination){
       $this->read($source);
       $this->write($destination);
     }
    }
    
    class FileUtil{
    use Copier;
    public function copyFile($source,$destination){
       $this->copy($source, $destination);
     }
    }
    

    2023-02-08 14:31 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有