2014-12-05 184 views
0

我有一個ChildClass,擴展爲ParentClassParentClass有一個構造函數,它帶有兩個參數__construct('value2', ParentClass::COMMON)。 HOwever我試圖從子類中調用繼承的newSelect()方法。到目前爲止它還沒有成功。我如何從ChildClass撥打NewSelect()?也有可能,即使ParentClass有一個構造函數需要兩個參數?在子類中訪問父級方法的正確方法

家長

class ParentClass {  

const COMMON = 'common'; 

protected $db; 

protected $common = false; 

protected $quotes = array(
'Common' => array('"', '"'), 
'Value2' => array('`', '`'), 
'Value3' => array('`', '`'), 
'Value4' => array('`', '`'), 
); 

protected $quote_name_prefix; 

protected $quote_name_suffix; 

    public function __construct($db, $common = null) { 
    $this->db = ucfirst(strtolower($db)); 
    $this->common = ($common === self::COMMON); 
    $this->quote_name_prefix = $this->quotes[$this->db][0]; 
    $this->quote_name_suffix = $this->quotes[$this->db][1]; 
    } 

public function newSelect() { 
    return $this->newInstance('Select'); 
} 

protected function newInstance($query) { 
    //Some more code, not relevant to example 
} 


} 

兒童

class ChildClass extends ParentClass {  



    public function __construct() { 

    } 

    // Call the inherited method 

    private $_select = Parent::newSelect(); 

    // Do something with it 

} 
+0

您是否收到任何錯誤或發生了什麼? – 2014-12-05 19:47:13

+0

@u_mulder是的,'解析錯誤:語法錯誤,意外'(',期待','或';''或者如果我在子類中創建一個與'new'對象我得到'解析錯誤:語法錯誤,意外'new''。 – 2014-12-05 19:49:11

+0

語法錯誤發生在某些行上,究竟是什麼行以及這些行上的代碼是什麼? – 2014-12-05 19:54:01

回答

1

//你不能做到這一點

private $_select = Parent::newSelect(); 

//試試這個

private $_select; 

public function construct($value, $common) 
{ 
    Parent::__construct($value, $common); 
    $this->_select = $this->newSelect(); 
} 

//和

$obj = new ChildClass('Value2', ParentClass::COMMON); // ParentClass::COMMON - not sure why you would do this 
$select = $obj->newSelect(); // this is being called in constructor, why call it again 

說實話,我甚至不知道你在做什麼,但是它的一切都是錯誤的!

+0

好吧,一旦我有機會在接下來的幾分鐘內測試一下哦,我可能沒有讓自己清楚,但忽略了'$ obj = new ChildClass()...' – 2014-12-05 21:30:32