2013-08-01 61 views
4

我有一個(抽象)父類應該提供建設期間的功能。子類可以覆蓋在構造中使用的屬性:PHP繼承︰子類重寫父變量/屬性在構造函數中使用

class Parent extends MiddlewareTest 
{ 
    // abstract channel properties 
    protected $title = NULL; 
    protected $type = NULL; 
    protected $resolution = NULL; 

    function __construct() { 
     parent::__construct(); 

     $this->uuid = $this->createChannel($this->title, $this->type, $this->resolution); 
    } 
} 

class Child extends Parent 
{ 
    // channel properties 
    protected $title = 'Power'; 
    protected $type = 'power'; 
    protected $resolution = 1000; 
} 

問題是,當其被不重寫運行Child::__construct()$this->createChannel被調用NULL參數)不使用覆蓋的性質。

這是可能的PHP中,或者我將不得不訴諸覆蓋子構造函數每次提供所需的功能?

注:我看到Properties shared between child and parents class in php但這是不同的,因爲子屬性沒有在構造函數中分配,而是根據定義。

更新

事實證明我的測試情況下是錯誤的。由於MiddlewareTest基於SimpleTest單元測試用例,因此SimpleTest實際上 - 我沒有意識到 - 通過自動運行實例化了從未滿足的Parent類本身。通過使父類抽象來修復。

經驗教訓:建立一個乾淨的測試用例,並在哭求求救之前實際運行它。

回答

2

我不確定你的服務器上發生了什麼。我不得不做出對MiddlewareTest類的假設,修改你的類名,並添加了一些簡單的調試線,但與此代碼:

<?php 
/** 
* I'm not sure what you have in this class. 
* Perhaps the problem lies here on your side. 
* Is this constructor doing something to nullify those properties? 
* Are those properties also defined in this class? 
*/ 
abstract class MiddlewareTest { 
    // I assume this properties are also defined here 
    protected $title = NULL; 
    protected $type = NULL; 
    protected $resolution = NULL; 
    protected $uuid = NULL; 

    public function __construct() 
    {} 

    protected function createChannel($title, $type, $resolution) 
    { 
     echo "<pre>" . __LINE__ . ": "; var_export(array($this->title, $this->type, $this->resolution)); echo "</pre>"; 
     echo "<pre>" . __LINE__ . ": "; var_export(array($title, $type, $resolution)); echo "</pre>"; 
     return var_export(array($title, $type, $resolution), true); 
    } 
} 

// 'parent' is a keyword, so let's just use A and B 
class A extends MiddlewareTest 
{ 
    // abstract channel properties 
    protected $title = NULL; 
    protected $type = NULL; 
    protected $resolution = NULL; 

    function __construct() { 
     parent::__construct(); 

     echo "<pre>" . __LINE__ . ": "; var_export(array($this->title, $this->type, $this->resolution)); echo "</pre>"; 
     $this->uuid = $this->createChannel($this->title, $this->type, $this->resolution); 
     echo "<pre>" . __LINE__ . ": "; var_export($this->uuid); echo "</pre>"; 
    } 
} 

class B extends A 
{ 
    // channel properties 
    protected $title = "Power"; 
    protected $type = "power"; 
    protected $resolution = 1000; 
} 

$B = new B(); 
?> 

我得到這些結果:

37: array (
    0 => 'Power', 
    1 => 'power', 
    2 => 1000, 
) 

20: array (
    0 => 'Power', 
    1 => 'power', 
    2 => 1000, 
) 

21: array (
    0 => 'Power', 
    1 => 'power', 
    2 => 1000, 
) 

39: 'array (
    0 => \'Power\', 
    1 => \'power\', 
    2 => 1000, 
)' 

正如你所看到的,這些值就像在實例化類中定義的那樣被傳遞,就像預期一樣。

你能否介紹一下你的MiddlewareTest類的一些細節,可能會揭示爲什麼你會遇到這種行爲?

你運行的是哪個版本的php?

+0

對不起,沒有提供一個工作的例子。你的提示讓我開始建立一個,我可以看到你的觀點。試圖找到現在的差異。 – andig

相關問題