如何在运行时(动态)创建PHP静态类属性? [英] How do I create a PHP static class property at runtime (dynamically)?
本文介绍了如何在运行时(动态)创建PHP静态类属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做这样的事情:
public static function createDynamic(){
$mydynamicvar = 'module';
self::$mydynamicvar = $value;
}
并且能够使用
$value = self::$module;
推荐答案
我不知道你为什么要这样做,但这样做。您必须像函数一样访问动态变量,因为PHP中没有__getStatic()魔术方法。
I don't know exactly why you would want to do this, but this works. You have to access the dynamic 'variables' like a function because there is no __getStatic() magic method in PHP yet.
class myclass{
static $myvariablearray = array();
public static function createDynamic($variable, $value){
self::$myvariablearray[$variable] = $value;
}
public static function __callstatic($name, $arguments){
return self::$myvariablearray[$name];
}
}
myclass::createDynamic('module', 'test');
echo myclass::module();
这篇关于如何在运行时(动态)创建PHP静态类属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文