嗨我有一個關於$ this的問題。php類擴展
class foo {
function __construct(){
$this->foo = 'bar';
}
}
class bar extends foo {
function __construct() {
$this->bar = $this->foo;
}
}
將
$ob = new foo();
$ob = new bar();
echo $ob->bar;
結果bar
??
我只問,因爲我認爲它會,但除了我的腳本似乎並沒有導致我的想法。
有點奇怪,但很靈活,因爲你可以很容易地根本過載(只調用父) ,部分重載構造函數(從新構造函數中調用它)或完全重載它(根本不調用它)。所以雖然與其他語言相比很奇怪,但這並不意味着它很奇怪(它可以被看作是一個巨大的好處)... – ircmaxell 2010-11-07 15:39:51
那麼這個擴展類被稱爲是沒有意義的嗎?我認爲這會帶來它的對象。 – 2010-11-07 18:30:04
不,$ this繼續引用當前實例 – 2010-11-07 20:08:56