note 123998 added to class.arrayaccess

From: Date: Mon, 01 Jul 2019 16:53:46 +0000
Subject: note 123998 added to class.arrayaccess
Groups: php.notes 
Request: Send a blank email to php-notes+get-211455@lists.php.net to get a copy of this message
An experimental check addition to the findings of Per (on offsetExists method) added here (8 years ago). <?php class obj implements ArrayAccess { private $container = array(); public function __construct() { $this->container = array( "one" => 1, "two" => 2, "three" => 3, ); } public function offsetSet($offset, $value) { print "offsetSet method Triggered"; if (is_null($offset)) { $this->container[] = $value; } else { $this->container[$offset] = $value; } } public function offsetExists($offset) { print "offsetExists method Triggered"; return isset($this->container[$offset]); } public function offsetUnset($offset) { print "offsetUnset method Triggered"; unset($this->container[$offset]); } public function offsetGet($offset) { print "offsetGet method Triggered"; return isset($this->container[$offset]) ? $this->container[$offset] : null; } } $obj = new obj; ## Assigning a Value $obj['two'] = '2'; // Output: offsetSet method Triggered ## Checking if array offset already set isset($obj['two']); // Output : offsetExists method Triggered ## Unsetting array value on the offset 'two' unset($obj['two']); // Output : offsetUnset method Triggered ## Accessing array value at offset 'two' return $obj['two']; // Output : offsetGet method Triggered ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.195 (proxied: 196.62.16.32) ---- Manual Page -- http://php.net/manual/en/class.arrayaccess.php Edit -- https://master.php.net/note/edit/123998 Del: integrated -- https://master.php.net/note/delete/123998/integrated Del: useless -- https://master.php.net/note/delete/123998/useless Del: bad code -- https://master.php.net/note/delete/123998/bad+code Del: spam -- https://master.php.net/note/delete/123998/spam Del: non-english -- https://master.php.net/note/delete/123998/non-english Del: in docs -- https://master.php.net/note/delete/123998/in+docs Del: other reasons-- https://master.php.net/note/delete/123998 Reject -- https://master.php.net/note/reject/123998 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211455) next »