class A {
public $arrayToReturn = array();
function __construct() {
$arrayToReturn['country'] = 'US';
}
}
class B extends A {
public function foo() {
print_r($arrayToReturn);
}
}
$b = new B();
$b->foo();
print_r($b->arrayToReturn);
結果:
PHP Notice: Undefined variable: arrayToReturn in xxxx.php on line 11
Array
(
)
我不能在B類的功能陣列,並獲得其應具有由$ B-一個元素> arrayToReturn調用一個空數組。爲什麼?
標記爲 「爲什麼這個代碼不工作」。請提供更多細節。 –
然後,當然,也是愚蠢的http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and –