[英]The class variable is undefined in class method
只需看下面的簡單代碼即可。 服務器告訴我wheel_count
函數car_detail()
中的car_detail()
的錯誤。 那么,為什么會這樣呢?
你能幫助我嗎?
<?php
class cars {
var $wheel_count = 4;
var $door_count = 4;
function car_detail() {
return $this->$wheel_count;
}
}
$bmw = new cars();
echo $bmw->car_detail();
?>
使用$this->wheel_count
訪問屬性:)
請注意,您有一個額外的“ $”。
如果您將屬性聲明為static
,則可以使用“ $”來訪問它們:
static $something = 'blah';
function getSomething()
{
return self::$something;
}
...或“超出”課堂:
$something = MyClass::$something;
另外,請考慮使用更高級的代碼編輯器/ IDE,該編輯器/ IDE會在您鍵入時讓您知道自己的代碼無效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.