php trait使用另一个特性

时间:2013-12-23 14:53:05

标签: php class oop traits

我有一个使用其他特性的特性,现在我遇到了类中不存在的函数的错误。 我简化了代码:

的settings.php:

<?php
trait settings{
    protected function getSetting($type, $setting){ // read setting from config.ini
        try{
            $configFile=dirname(__FILE__)."/../config.ini";
            if(!file_exists($configFile)||!is_file($configFile))throw new Exception("Config file was not found. ");
            $configContents=parse_ini_file($configFile,true);
            if(is_array($configContents)&&array_key_exists($type,$configContents)&&is_array($configContents[$type])&&array_key_exists($setting,$configContents[$type]))return $configContents[$type][$setting];
            else throw new Exception("Setting ".$setting." could not be found in ".$type.".");
        }
        catch(Exception $e){throw new Exception($e->getMessage());}
    }
}
?>

database.php中

<?php
trait database{
    use settings,session;
    private $pdo;
    protected function connect(){ // connect to database
        try{
            $this->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

<?php
class users{
    use database;
    public function __construct(){
        try{
            $this->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 个答案:

答案 0 :(得分:15)

也许是因为readSetting实际上被称为getSetting?

答案 1 :(得分:9)

  

代码重用是面向对象编程最重要的方面之一。

Multiple TraitsComposing Multiple Traits的一个简单示例,您可以通过该示例轻松分析您的情况。

  1. 使用多种特征
  2. 一个类可以使用多个特征。以下示例演示如何在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();
    
    1. 撰写多个特征
    2. 通过在特征声明中使用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);
       }
      }