-1
我想訪問父類變量。我如何訪問父類變量? 這裏是例子:
我如何訪問父類變量?
class myclass {
var $parentvar;
function __construct() {
$this->parentvar = 'Some text or array';
}
}
class anotherClass extends myclass {
function __construct(argument)
parent::__construct();
}
function print_var() {
echo $parent->parentvar;
}
}
請建議我
應該補充的是,當你不指定'protected','public'或'private'時,默認變量/函數爲'public'。 [*類屬性必須定義爲public,private或protected。如果使用var聲明,該屬性將被定義爲public。*](http://php.net/manual/en/language.oop5.visibility.php)。 OP,除非你使用'$ argument'作爲參數,否則你不必使用'parent :: __ construct'。 – h2ooooooo
只要你在子類中定義一個構造函數,父類不會自動調用,所以如果這是一個完整的類,那麼子構造函數就可以被刪除。我認爲這是一個不完整的例子。 – shanethehat
我假設它是不完整的,因爲'$ argument'只在子類中指定(雖然它可以用func_get_args獲得變量,即使它沒有在函數中指定,儘管這是一個可怕的解決方案)。 – h2ooooooo