2013-12-23 4 views
3

다른 특성을 사용하는 특성을 가지고 있으며 클래스에 존재하지 않는 함수에 대한 오류가 발생합니다. 내가 코드를 단순화 :다른 특성을 사용하는 PHP 특성

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

그리고 지금은이 오류를 받고 있어요 :

Fatal error: Call to undefined method users::readSetting() in /home/deb2371/domains/nonamenohistory.com/public_html/include/classes/class.database.php on line 18

내가 생각하는 것 : 클래스 사용자는 특성 데이터베이스를 사용하고 특성 데이터베이스는 특성 설정 및 특성 세션을 사용합니다.

오류가 발생하지 않았지만 불행히도 이러한 경우는 아닙니다.

누군가이 문제를 해결하는 방법을 알고 있습니까?

+0

왜 형질을 사용하지 않아야합니까? 설명 해주십시오. – SheperdOfFire

+0

솔리드 원칙의 위반에 대해 엄청난 혼란을 겪었을뿐 아니라 통역사가 지원하는 복사 붙여 넣기로도 구현됩니다. –

+0

나는 그것이 왜 그런지 이해하지 못합니다. – SheperdOfFire

답변

14

아마도 readSetting이 실제로 getSetting이라고 할 수 있습니까?

+1

아우 ... 당신 말이 맞아요. 다른 클래스의 코드를 복사하여 변경하는 것을 잊어 버렸습니다. 내가 그걸 놓쳤다는 느낌이 너무 어리 석다. 고맙습니다. – SheperdOfFire

+7

그 느낌을 알고 ... – David

관련 문제