PHP returns a static variable by reference

Ignore namespaces etc. Can someone explain why I cannot return a reference to my static array? Effectively a class is a getter and setter. I wanted to use static methods since the class would never need to be created again during the application life cycle.

I understand what I am doing may just be โ€œbad practiceโ€ - more knowledge on this subject will be appreciated.

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" ); } } } 

Using it like that

 $test = array("my","array"); \xtend\core\classes\registry::add("config",&$test); $test2 =& \xtend\core\classes\registry::get("config"); $test2[0] = "notmy"; print_r($test); 

Suppose i come back

 array("notmy","array"); 

But I will just return the original.

+4
source share
3 answers

Summary:

 class Registry { private static $global_registry = array(); public static function Add($key, &$value){ static::$global_registry[$key] =& $value; } public static function &Get($key){ return static::$global_registry[$key]; } public static function Remove($key){ unset(static::$global_registry[$key]); } } $test = array("my", "array"); Registry::Add("config", $test); $test2 =& Registry::Get("config"); $test2[0] = "notmy"; var_dump($test); 

It is really very simple, once you understand how it works:

  • Firstly, the add function must follow the link , otherwise the value that is visible in the function is not even the value that you passed.

  • Secondly, when saving the value in $global_registry we must assign by reference . Otherwise, the stored value is not even the value observed in the function.

  • Third, we must return by reference , putting an ampersand in a function declaration. You have already done this, except for this code:

 $ref =& self::$global_registry[$key]; // redundant line return $ref; 

Same as this code:

 return self::$global_registry[$key]; 

Since in the line public static function &get we have already announced that the return value is a reference.

  • And finally, we need to assign a return link to the link that you also made:
 $test2 =& Registry::Get("config"); 

As you can see, the whole chain should be a link. If any step is not taken by the referee, this will not work.

+1
source

You can return your static array by deferring:

 $ref = &self::$global_registry[$key]; 

You need to access it as follows:

 $keyVal = &registry::get($key); 
0
source

You can use the following training code for links

 class Apple { public static $basket = [], $scripts =[]; public static function addToBasket($grapes) { self::$scripts =& self::AppStatic(__CLASS__ . __METHOD__, array()); self::$scripts[$grapes] = ['type' => $grapes]; return self::$scripts; } public static function &AppStatic($name, $default_value = NULL) { if (isset(self::$basket[$name]) || array_key_exists($name, self::$basket)) { return self::$basket[$name]; } if (isset($name)) { return self::$basket[$name] = &$default_value; } $data = NULL; foreach (self::$basket as $key => $value) { $data[$key] = $value; } return $data; } } 
-1
source

Source: https://habr.com/ru/post/1440937/


All Articles