如果我在 PHP 类中使用“点”为变量赋值,则会失败。
例如:
class bla {
public $a = 'a' . 'b';
}
否则我应该如何处理这个问题?
最佳答案
您只能在构造函数中执行此操作,因为必须在使用常量表达式声明时初始化类变量/属性。来自manual :
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
这意味着您不能使用任何运算符或函数调用。
class bla {
public $a;
public function __construct() {
$this->a = 'a' . 'b';
}
}
关于php - 如何在 PHP 中的对象变量中执行文本连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5051553/