在设置类属性时调用函数 [英] Call a function while setting class properties

查看:122
本文介绍了在设置类属性时调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP(5.2或5.3)中设置类属性时访问函数?

Is accessing functions while setting class properties possible in PHP (5.2 or 5.3) ?

class DAOClass {

   var $someProperty = SomeObject::staticMethod('readConfigProperty');

}


推荐答案

不可能,因为您必须使用常量值初始化属性。甚至不可能这样做:

It is not possible because you have to initialize the properties with constant values. It isn't even possible to do this:

var $property = array(0);

你想做什么的方法是在类构造函数中:

The way to do what you want to do is inside the class constructor:

class DAOClass {

    var $someProperty;

    public function __construct() {
         $this->someProperty = SomeObject::staticMethod('readConfigProperty');
    }
}

注意,使用 var 声明属性不是首选方法。改为使用 private protected public 以及其可见性( var 默认为 public )。

As a side note, using var to declare properties is not the preferred way. Use private, protected or public instead to declare a property along with its visibility (var defaults to public).

这篇关于在设置类属性时调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆