2009-04-12 90 views
1

你好,我想就班

我想要做一个超类的东西哪一个是我的所有类扩展它

  ____ database class 
     /
chesterx _/______ member class 
      \ 
      \_____ another class 

我想打电话给那就是在这样

$this->database->smtelse(); 



class Hello extends Chesterx{ 

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

} 

数据库类中的方法,我想调用一个方法与其父类的名字时,我在我的超类扩展到任何级别

回答

2

我不太清楚你的最后一句话是什么意思,但是这是完全正确的:

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

我已经解决了我的问题,你的答案感谢您的帮助 – 2009-04-12 18:09:30

0

你也可以考虑使用方法来获取子对象 的好处是对象不会被初始化直到他们需要它,并且还提供了一个更松散耦合的代码,可以让你改变数据库的方式初始化更容易。

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

}