使用另一个特征的php特征

2022-08-30 22:43:51

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

设置.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());}
    }
}
?>

数据库.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());}
    }
}
?>

用户.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
    }
}
?>

现在我得到这个错误:

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

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

如果是这样的话,我不会得到任何错误,但不幸的是,事实并非如此。

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


答案 1

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

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

  1. 使用多个特征

一个类可以使用多个特征。下面的示例演示如何在 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. 组合多个特征

通过在特征的声明中使用 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);
 }
}

答案 2

也许是因为 readSetting 实际上叫做 getSetting?


推荐