English 中文(简体)
如何宣布某一类别中的公共变量
原标题:How to declare a public variable variable in a class

Hey I have a class like this

class derp{

public $$ini;

public static function regIni($ini){

derp::$$ini =  test ;

}

}

之后,我又在另一处档案中发现:

core::regIni( register );

之后,我使用其他部分。

core::$register;

造成错误

public $$ini 

不是有效的法典,但如果我离开,我不能确定

core::$$ini

我如何确定这一点?

请注意,尼元是一个可变的意思,即尼元的价值是按变名称计算,即基尼 = 登记处,而基尼实际汇率为美元。

最佳回答

因此,这不是一个过于有益的答案,因为我只能得出结论,目前是不可能的。

  • You cannot define a new ReflectionProperty("derp", "static_prop") for example and attach it. It s really for introspection only.
  • $c = new ReflectionClass("derp"); and $derp->setStaticPropertyValue("p", 123); is not working either. The properties need to be predefined still.
  • And lastly, neither can the runkit_* functions help with this task. They are intended for changing methods mainly.
  • Same for classkit.

I m not aware of other such PECL extensions, but that wouldn t be useful as general solution anyway. So for current PHP versions you cannot add static class properties after the parsing stage.

问题回答

Why not use access methods for setting and getting class data?

class derp {

    protected static $_data = array(); 

    public static function regIni($ini, $value) {
         derp::$_data[$ini] = $value;   
    }

    public static function getIni($ini, $default = NULL) {
        return isset(derp::$_data[$ini]) ? derp::$_data[$ini] : $default;
    }
}

take a look at magic methods __set and __get

Mario说:

It s not doable. Static properties can only be defined at the parsing stage. Neither ReflectionProperty or ReflectionClass::setStaticPropertyValue, nor runkit_* functions are currently capable (intended) to create static class properties. Sorry

Geuss i l settle for a work around then. Made an array $ini and loaded the values into there derp:$ini[ base ][ key ]

感谢援助,

页: 1





相关问题
Benefits of declaring a function as "inline"?

Every time I read about the "inline" declaration in C it is mentioned that it is only a hint to the compiler (i.e. it does not have to obey it). Is there any benefit to adding it then, or should I ...

热门标签