$foo = isset($array['foo']) ? $array['foo'] : null; $bar = isset($array['bar']) ? $array['bar'] : null;
$foo = $array['foo'] ? : null; $bar = $array['bar'] ? : null;
error_reporting = E_ALL
is set on the development server. And it is precisely in such cases that ArrayObject comes to the rescue - a class whose objects can be accessed using the syntax of arrays and allowing you to change the behavior using the inheritance mechanism.ArrayObject
heirs:DefaultingArrayObject
- returns the default value if the key is not defined in the array;ExceptionArrayObject
- throws an exception if the key is not defined in the array;CallbackArrayObject
— Array values are functions (closures) that return some value.dict.get(key, default)
- if the key is not defined in the array - the default value is returned. This works great when the default values of all elements to which we access are the same, and not so elegant when we want to get different values in the case of a missing key. The full listing of this class is as follows: class DefaultingArrayObject extends \ArrayObject { protected $default = null; public function offsetGet($index) { if ($this->offsetExists($index)) { return parent::offsetGet($index); } else { return $this->getDefault(); } } /** * @param mixed $default * @return $this */ public function setDefault($default) { $this->default = $default; return $this; } /** * @return mixed */ public function getDefault() { return $this->default; } }
$array = new DefaultingArrayObject($array); $foo = $array['foo']; $bar = $array['bar'];
$array = new DefaultingArrayObject($array); $foo = $array->setDefault('default for foo')['foo']; $bar = $array->setDefault('default for bar')['bar'];
if (isset($array['foo']) && isset($array['bar'] && isset($array['baz'])) { // logic that uses foo, bar and baz array values } else { // logic that does not use foo, bar and baz array values }
$array = new ExceptionArrayObject($array); try { // logic that uses foo, bar and baz array values } catch (UndefinedIndexException $e) { // logic that does not use foo, bar and baz array values }
class ExceptionArrayObject extends \ArrayObject { public function offsetGet($index) { if ($this->offsetExists($index)) { return parent::offsetGet($index); } else { throw new UndefinedIndexException($index); } } }
class UndefinedIndexException extends \Exception { protected $index; public function __construct($index) { $this->index = $index; parent::__construct('Undefined index "' . $index . '"'); } /** * @return string */ public function getIndex() { return $this->index; } }
$array = new CallbackArrayObject([ 'foo' => function() { return 'foo ' . uniqid(); }, 'bar' => function() { return 'bar ' . time(); }, ]); $foo = $array['foo']; // "foo 526afed12969d" $bar = $array['bar']; // "bar 1382743789"
class CallbackArrayObject extends \ArrayObject { protected $initialized = array(); public function __construct(array $values) { foreach ($values as $key => $value) { if (!($value instanceof \Closure)) { throw new \RuntimeException('Value for CallbackArrayObject must be callback for key ' . $key); } } parent::__construct($values); } public function offsetGet($index) { if (!isset($this->initialized[$index])) { $this->initialized[$index] = $this->getCallbackResult(parent::offsetGet($index)); } return $this->initialized[$index]; } protected function getCallbackResult(\Closure $callback) { return call_user_func($callback); } }
$array = new ConfigurableCallbackArrayObject([ 'foo' => function($config) { return 'foo ' . $config['foo']; }, 'bar' => function($config) { return 'bar ' . $config['bar']; }, ]); $array->setConfig(['foo' => 123, 'bar' => 321]); $foo = $array['foo']; // "foo 123" $bar = $array['bar']; // "bar 321"
class ConfigurableCallbackArrayObject extends CallbackArrayObject { protected $config; protected function getCallbackResult(\Closure $callback) { return call_user_func($callback, $this->getConfig()); } public function setConfig($config) { $this->config = $config; } public function getConfig() { return $this->config; } }
Source: https://habr.com/ru/post/199096/
All Articles