class A {
private $aa;
protected $bb = 'parent bb';
function __construct($arg) {
//do something..
}
private function parentmethod($arg2) {
//do something..
}
}
class B extends A {
function __construct($arg) {
parent::__construct($arg);
}
function childfunction() {
echo parent::$bb; //Fatal error: Undefined class constant 'bb'
}
}
$test = new B($some);
$test->childfunction();
問題: 如何在子顯示父變量? 預期結果將回聲「父BB」PHP訪問父類變量
因此,這意味着**父**關鍵字只用於訪問父方法? – Kuntau
通常,當你想覆蓋父方法時,你會使用parent ::,但仍然引用父代的功能。如果你只是想調用父節點的方法,你可以像變量一樣進行:'$ this-> parentmethod()' –