无法在类中将变量设置为等于函数 [英] Cannot set variable in class equal to a function
本文介绍了无法在类中将变量设置为等于函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我今天开始学习面向对象的PHP编程,并且正在努力解决以下问题:
I started today with learning object oriented PHP programming and I am struggling with the following problem:
我可以将变量设置为例如10:
class exampleClass {
private $number = 10;
}
但是我不能将其设置为等于返回10的函数:
class exampleClass {
private $number = exampleFunction();
}
推荐答案
您无法设置课程属性直接作为表达式:
You can't set class properties directly as expressions:
无效:
class Test {
protected $blah = 1 + 1;
}
而是将其设置在类构造中:
Instead set it in the class construct:
class Test {
protected $blah;
public function __construct() {
$this->blah = 1 + 1;
}
}
这篇关于无法在类中将变量设置为等于函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文