类方法中未定义类变量


The class variable is undefined in class method

只需看看下面的简单代码。服务器告诉我未定义函数car_detail()中的wheel_count的错误。那么,为什么会是这样呢?

你能帮我吗?

<?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,它可以让您在键入代码后立即知道您有无效代码。