使用另一个特征的php特征
我有一个使用另一个特征的特征,现在我收到关于类中不存在的函数的错误。我简化了代码:
设置.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 行
我以为会发生这样的事情:类用户使用特质数据库,特质数据库将使用特质设置和特质会话。
如果是这样的话,我不会得到任何错误,但不幸的是,事实并非如此。
有人知道如何解决这个问题吗?