2009-04-12 31 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; 
    } 

}