忽略命名空间等任何一个可以解释为什么我不能返回对我的静态数组的引用?这个班级实际上是一个吸气和二传手。我想使用静态方法,因为在整个应用程序生命周期中,类永远都不需要再次实例化。PHP通过引用返回静态变量
我明白我在做什么可能只是“不好的做法” - 任何关于此事的更多知识将不胜感激。
namespace xtend\core\classes;
use xtend\core\classes\exceptions;
class registry {
private static $global_registry = array();
private function __construct() {}
public static function add($key, $store) {
if (!isset(self::$global_registry[$key])) {
self::$global_registry[$key] = $store;
} else {
throw new exceptions\invalidParameterException(
"Failed to add the registry. The key $key already exists."
);
}
}
public static function remove($key) {
if (isset(self::$global_registry[$key])) {
unset(self::$global_registry[$key]);
} else {
throw new exceptions\invalidParameterException(
"Cannot remove key $key does not exist in the registry"
);
}
}
public static function &get($key) {
if (isset(self::$global_registry[$key])) {
$ref =& self::$global_registry[$key];
return $ref;
} else {
throw new exceptions\invalidParameterException(
"Cannot get key $key does not exist in the registry"
);
}
}
}
使用它像这样
$test = array("my","array");
\xtend\core\classes\registry::add("config",&$test);
$test2 =& \xtend\core\classes\registry::get("config");
$test2[0] = "notmy";
print_r($test);
你会假定我还是会回到
array("notmy","array");
但我只是找回当初。
您可以通过注册表访问::您是否正确地使用它添加($键,$商店) –
?见[php.net参考资料](http://php.net/manual/en/language.references.return.php) –
@AlvinWong更新我的问题与我如何使用它。 –