<?php /** * This class implements ArrayAccess and the magic * isset method to test the behavior of certain operators * * It simulates having just a property called a */ class Test implements ArrayAccess { public function __isset($property) { return $property === 'a'; } public function __get($property) { return $property === 'a'? 'a' : null; } public function offsetExists ( $offset ) : bool { return $offset === 'a'; } /** * We are required by the interface to implement these methods * but they're irrelevant to our test */ public function offsetGet ( $offset ) { return $offset === 'a'? 'a' : null; } public function offsetSet ($offset , $value ) : void { return; } public function offsetUnset ($offset ) : void { return; } } /** * Start performing the actual tests, we wanna compare the * behavior of isset() and the ?? operator */ $tests = [ 'isset->a' => function($e) { return isset($e->a) && $e->a? $e->a : false; }, 'isset->b' => function($e) { return isset($e->b) && $e->b? $e->b : false; }, 'isset[a]' => function($e) { return isset($e['a']) && $e['a']? $e['a'] : false; }, 'isset[b]' => function($e) { return isset($e['b']) && $e['b']? $e['b'] : false; }, '[a]??false' => function($e) { return $e['a']??false; }, '[b]??false' => function($e) { return $e['b']??false; }, '->a??false' => function($e) { return $e->a??false; }, '->b??false' => function($e) { return $e->b??false; } ]; $a = new Test(); foreach ($tests as $caption => $fn) { echo $caption, ': ', ($fn($a)? $fn($a) : 'false'), PHP_EOL; }
You have javascript disabled. You will not be able to edit any code.
Value for `_results` contains invalid data `array`