我在课堂上遇到动态变量问题;
<?
class test {
public static function set($key, $value) {
self::$$key = $value;
}
}
test::set('testKey', 'testValue');
?>
如何设置变量,然后访问 test::$testKey ?
一段时间以后:
<?
class test {
public static $dynamic;
public static function set($key, $value) {
self::$dynamic->$key = $value;
}
public static function __callStatic($method, $agrs) {
echo self::$dynamic->$method;
}
}
test::$dynamic = new test();
test::set("hey", "test");
test::hey();
?>
这个解决方案怎么样?