2013-06-20 25 views
0

重載父的方法,我有PHP代碼如何調用它在孩子

<? 
class AParent { 

    function to_s() { 
    return $this->id; 
    } 

} 

class AChild { 

    public $id; 
    public $name; 

    function to_s() { 
    if(!empty($this->name)) { 
     $ret = $this->name; 
    } else { 
     $ret = /** call parent's method to_s() */ 
    } 
    return $ret; 
    } 

} 

如何調用該兒童父母重載方法? :)這是可能的靜態方法,但如何使用非靜態方法呢?

$a = new AChild(); 
$a->id = 1; 
$a->name = 'Something'; 
echo $a; // Should echo "Something" 

$b = new AChild(); 
$b->id = 2; 
echo $b; // Should echo "2" 
+0

旁註:你可以移動'公共的$ id;'申報到'AParent'類。 – Voitcus

回答

0

使用$ret = parent::to_s(),這是不夠

+0

我希望它與「 - >」。 「::」作品:)。謝謝。 – Willmore

相關問題