Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
$this->;a->;b->;c->;在php中从超类调用方法_Php_Oop - Fatal编程技术网

$this->;a->;b->;c->;在php中从超类调用方法

$this->;a->;b->;c->;在php中从超类调用方法,php,oop,Php,Oop,你好,我想在课堂上做点什么 我想做一个超级类,其中一个是我所有的类都在它上面扩展 ____ database class / chesterx _/______ member class \ \_____ another class 我想像这样调用数据库类中的方法 $this->database->smtelse(); class Hello extends Chesterx{

你好,我想在课堂上做点什么

我想做一个超级类,其中一个是我所有的类都在它上面扩展

            ____  database class
           /
chesterx _/______  member class
          \
           \_____  another class   
我想像这样调用数据库类中的方法

$this->database->smtelse();



class Hello extends Chesterx{

  public function ornekFunc(){
     $this->database->getQuery('popularNews');
     $this->member->lastRegistered();
  }

}  

当我将我的超类扩展到任何类时,我想用它的父类名调用一个方法,我不太清楚你最后一句话的意思,但这是完全正确的:

class Chesterx{
 public $database, $member;
 public function __construct(){
   $this->database = new database; //Whatever you use to create a database
   $this->member = new member;
 }
}

考虑单例模式——它通常更适合数据库交互

>你也可以考虑使用方法来获取子对象 这样做的好处是,对象在需要时才进行初始化,而且还提供了一个更松散的耦合代码,使您可以更轻松地更改数据库的初始化方式

class Chesterx{
   public $database, $member;

   public function getDatabase() {
       if (!$this->database ) {
           $this->database = new database; //Whatever you use to create a database
       }
       return $this->database;
   }

   public function getMember() {
       if (!$this->member) {
           $this->member = new member;
       }
       return $this->member;
   }

}

我很想知道原因。