使用另一个trait的php trait

23
我是一个有用的助手,可以为您翻译文本。

我有一个使用另一个trait的特性,现在我收到了关于类中不存在的函数的错误。

我已经简化了代码:

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
    }
}
?>

现在我遇到了这个错误:

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

我的预期是这样的: 类users使用trait database,而trait database会使用trait settings和trait session。

如果是这种情况,我就不会遇到任何错误,但不幸的是事实并非如此。

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


为什么我不应该使用traits?请解释。 - user936965
因为除了在SOLID原则方面存在严重问题之外,它们实际上还是基于解释器辅助的复制粘贴实现的(在底层)。 - tereško
我不明白为什么会这样。 - user936965
3
何必捕获异常,只是为了抛出具有相同信息的新异常呢? - Justin E
你能否更新一下你的问题,说明它是可以实现的,这样我们就不会浪费时间在阅读一个语法错误上了? ;) - Daniel
2个回答

42

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

下面是一个使用多个Traits组合多个Traits的简单示例,可以帮助您轻松分析自己的情况。

  1. 使用多个Traits

一个类可以使用多个Traits。以下示例演示了如何在IDE类中使用多个Traits。它模拟了C编译模型,并为演示而在PHP中实现。

<?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
显然,考虑到这个问题的通用标题,这个答案将是99%访问者的正确答案。感谢您提供了详细的例子!(在我的情况下,我只是因为自己的“语法错误”而来到这里,但是您的例子澄清了我已经正确配置了我的特征,并且我找到了问题,正如预期的那样) - jerclarke
class FileUtil { use Reader; use Copier; … }(其中Copier已经包含Reader)是错误的吗?还是会发生什么情况? - luckydonald
@luckydonald - 你能在这里写出你遇到的错误吗? - Mohd Belal
我在询问它是否有效。从我所看到的来看,实际上并不是一个错误,并且可以包含相同的特征两次。 - luckydonald

22
你的问题可能是因为你使用了readSetting,但实际上应该叫做getSetting

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接