我有一个静态方法的类。静态方法返回一个私有静态 stdClass 对象。
myclass::get() // returns stdClass object
myclass::get()->name // name is hardcoded into the class
我将如何更改名称的值,例如:
myclass::get()->name = 'bob';
并设置好了吗?
我尝试像这样返回对象:
return &self::$static_object;
但这会引发语法错误。
我能做些什么?
编辑发布的代码以进行澄清
final class config {
private static $configs = array();
public static function get($config_name) {
if (isset($configs[$config_name])) {
return self::$configs[$config_name];
}
$file = __get_file_exists(M_CONFIGS . $config_name, 'conf.');
if ($file) {
$config = self::__scope_include($file);
if (!is_array($config) && !$config instanceof stdClass) {
/*
*
*
* FIX
*
*
*
*/
die('ERROR config.php');
}
return self::$configs[$config_name] = self::__to_object($config);
}
}
private static function __scope_include($file) {
return include $file;
}
private static function __to_object($config) {
$config = (object) $config;
foreach ($config as &$value) {
if (is_array($value)) {
$value = self::__to_object($value);
}
}
return $config;
}
}
echo config::get('people')->name; //dave
config::get('people')->name = 'bob';
echo config::get('people')->name; // should be bob, is dave