在 PHP(5.2 或 5.3)中可以在设置类属性的同时访问函数吗?
class DAOClass {
var $someProperty = SomeObject::staticMethod('readConfigProperty');
}
最佳答案
这是不可能的,因为您必须使用常量值初始化属性。甚至不可能这样做:
var $property = array(0);
做你想做的事情的方法是在类构造函数中:
class DAOClass {
var $someProperty;
public function __construct() {
$this->someProperty = SomeObject::staticMethod('readConfigProperty');
}
}
作为旁注,使用 var
声明属性 is not the preferred way .使用 private
、protected
或 public
来声明属性及其可见性(var
默认为 公共(public)
)。
关于php - 在设置类属性时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5475228/