Php类变量作为函数参数


Php class Variable as function parameter

如何使用类变量作为函数参数?

的例子:

class Test {
    protected $img = '';
    protected function foo($var = $this->img)
    {
       ... some code ...
    }
}

所以这是好的做法吗?

我不是百分之百确定,但我想你不能。您可以这样做:

class Test {
    protected $img = '';
    protected function foo($var = null)
    {
        if(is_null($var))
            $var = $this->img;
       ... some code ...
    }
}