Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I really wanted to do something like that, I'd use a static array instead:</p> <pre><code>function set($key, $value, $default = null) { static $values = array(); $values[$key] = is_null($value) ? $default : $value; } </code></pre> <p>If you want to also be able to access this data, a better idea is to encapsulate it in a class.</p> <pre><code>class Registry { private static $values = array(); public static function set($key, $value, $default = null) { self::$values[$key] = is_null($value) ? $default : $value; } public static function get($key, $default = null) { return array_key_exists($key, self::$values) ? self::$values[$key] : $default; } } </code></pre> <p>Then you can use the class statically:</p> <pre><code>Registry::set('key', 'value', 'default'); $value = Registry::get('key', 'default'); </code></pre> <p>An even better way of doing this is to not use the <code>static</code> keyword at all, and instead make a normal class with normal properties, then make a single instance of it and use that instance.</p> <pre><code>class Registry { private $values = array(); public function set($key, $value, $default = null) { $this-&gt;values[$key] = is_null($value) ? $default : $value; } public function get($key, $default = null) { return array_key_exists($key, $this-&gt;values) ? $this-&gt;values[$key] : $default; } } </code></pre> <p>You can then make an instance of the class and pass it along:</p> <pre><code>$registry = new Registry(); do_something_that_requires_the_registry($registry); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload