我有一个接近于此的问题:
很明显,我们可以像这样制作可选参数:
function myFunction($param='hello')
但我想在类的方法中使用它$this->something
而不是“hello”,它看起来像这样:
public function myFunction($param=$this->property)
但我得到一个:
Parse error: syntax error, unexpected '$this'
有没有可能得到它?
你需要设置为空然后检查
class example {
private $something = "something";
public function myFunction($a = null)
{
if($a === null)
$a = $this->something;
// more code goes here
return $a;
}
}
$test = new example();
print $test->myFunction();
// prints "something"
print $test->myFunction("hello");
// prints "hello"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句