2011-08-02 22 views
0

私はプライマリクラスと拡張クラス(データベース接続)を持っています。拡張クラスメソッドの呼び出し問題

親クラス名Classica
拡張私は親の内部の拡張クラスメソッドは呼び出すことができますどのようにDatabaseQ

これが機能していません。

$this->connectdb(); 

またはこの:

$this->DatabaseQ->connectdb(); 

コード例:

class DatabaseQ extends Classica{ 

    public $dbhost; 
    public $dbname; 
    public $dbuser; 
    public $dbpass; 

    function __construct(){ 
     include('config.php'); 
     $this->dbhost = $dbhost; 
     $this->dbname = $dbname; 
     $this->dbuser = $dbuser; 
     $this->dbpass = $dbpass; 
    } 

    #connect to database 
    public function connectdb(){  
     $link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass); 
     if (!$link) { 
      die('Could not connect: ' . mysql_error()); 
     }else { 
      //echo 'Connected Successfully to Database<br>'; 
     } 
     @mysql_select_db($this->dbname) or die("Unable to select database!"); 
    } 

    #read database 
    function readdb(){   
    }  
    #update database 
    private function updatedb(){   
    } 
    #close database connection 
    function closedb(){ 
     mysql_close();  
    } 

} 
を拡張

class Classica{ 

     function sample_method(){ 
      //connect db here 
      //run some sql queries here 
     } 
+1

'$ this-> connectdb();'は、非静的メソッドを拡張した場合、オブジェクトが 'DatabaseQ'オブジェクトの場合、親の関数で動作するはずです。あなたはそれが拡張クラスを呼び出すとは思わないその関数には何がありますか? – Wrikken

+0

実際に拡張/子クラスをインスタンス化して、その子クラスのインスタンス上で関数を呼び出していますか? – Endophage

+0

@Endophageいいです。どのようにして、親クラス内で拡張クラスを構築してメソッドを使用できるのでしょうか? – Codex73

答えて

1

あなたのクラスの両方の内容から判断すると、あなたはそれがすべてのラップ持つことができたときに、すべてのあなたが必要以上にいくつかのクラスを超える機能的責任を広めているのでClassicaクラスのいずれかの使用を作るためにのために絶対に理由はありません1つのクラスの中で

「親」クラスを使用している唯一の理由は、データベースに接続して初期クエリを実行することだけです。あとでいくつかの高度なデザインパターンを実装する予定がない限り、DatabaseQコンストラクタ内でこれを実行できない理由はまったくありません。一方

class DatabaseQ { 
    public $dbhost; 
    public $dbname; 
    public $dbuser; 
    public $dbpass; 

    function __construct(){ 
     include('config.php'); 
     $this->dbhost = $dbhost; 
     $this->dbname = $dbname; 
     $this->dbuser = $dbuser; 
     $this->dbpass = $dbpass; 

     $this->connectdb(); // This is a good place to initiate your DB connection 
     $this->doOtherInitStuff(); // Calling the rest of the init stuff. 
    } 

    /** 
    * This is the place where you do all of your init stuff. 
    * Note the private status! The environment doesn't need to have access to your DB initialization stuff 
    */ 
    private function doOtherInitStuff() { 
     // Do init stuff 
    } 

    #connect to database 
    private function connectdb(){ // Note the private scope too! Only the object itself needs to know how to connect to the db! 
     $link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass); 
     if (!$link) { 
      die('Could not connect: ' . mysql_error()); 
     }else { 
      //echo 'Connected Successfully to Database<br>'; 
     } 
     @mysql_select_db($this->dbname) or die("Unable to select database!"); 
    } 

    #read database 
    function readdb(){   
    }  
    #update database 
    private function updatedb(){   
    } 
    #close database connection 
    function closedb(){ 
     mysql_close();  
    } 

} 

あなたは別のDB「ドライバー」(オーバーロードされたメソッドを持つ拡張クラスは)あなただけが含まれます抽象クラスを作成することができましょうために、後に使用される基本クラスを作成する場合あなたの拡張された(ドライバ)クラスが実装する必要があるすべてのメソッドの青写真。

しかし、それは少し高度な話:)

EDITです:あなたは、具体的DatabaseQが、その後DatabaseQの拡張クラスを作成し、その中にそのすべてを置く取得するものを出力するために使用されるクラスが必要な場合はデータを吐き出す

class DatabaseQOutput extends DatabaseQ { 
    public function __construct(){ 
     parent::__construct(); // You make sure here that the parents constructor is executed and a DB connection and initialization stuff is taken care off 
    } 

    public function output() { 

    } 
} 

$db = new DatabaseQOutput(); 
$db->output(); 

しかし、あなたが一般的にそれが彼らの仕事ではありませんので、あなたが実際にあなたのデータベースの特定のクラスのいずれかがデータを出力するための責任を負うことにしたくない真実を伝えるために。 MVCを使用していないにもかかわらず、データベースクラスはモデルと見なす必要があります。つまり、データベースの抽象化レイヤーとすべてのデータフェッチ/送信操作の役割を主に担うことを意味します。

私があなただったら、あなたのデータベースクラスで取り出されたデータを出力するタスクが特別に用意されたクラスを作成します。そのようにして、ビューとしての役割を果たすクラスを作成し、データを出力するすべての責任を引き受けます。

+1

私の編集を見てください。少し長いですが、クラスの責任と組織を潜在的に再考するのに十分な情報を提供していると思います。 – brezanac

+0

うわー。これは素晴らしいです!このような素晴らしい説明のためにとてもホロドックに感謝します。 – Codex73

1

抽象メソッドを使用します。 OOPに関する本を読んでください。

abstract class Classica { 
    public abstract function connectdb(); 
    public function Test() { 
    $this->connectdb(); 
    } 
} 

class DatabaseQ extends Classica { 
    public function connectdb(){ 
    echo 'connected!'; 
    } 
} 

$x = new DatabaseQ(); 
$x->Test(); // output: 'connected!' 
+0

私はあまりにも考えることができます。 – Bretticus

+1

"OOPで本を読む"が大好き!私はそれを建設的なものにするだろうと思う。 – Codex73

関連する問題