note 108687 added to class.arrayobject

From: Date: Thu, 17 May 2012 17:51:21 +0000
Subject: note 108687 added to class.arrayobject
Groups: php.notes 
Request: Send a blank email to php-notes+get-188040@lists.php.net to get a copy of this message
Please note, that there's another potential "gotcha" in using ArrayObject like regular array. Objects are reference types and arrays are value types. Consider following piece of code: <?php class MyArrayObject extends ArrayObject { protected $data = array(); public function offsetGet($name) { return $this->data[$name]; } public function offsetSet($name, $value) { $this->data[$name] = $value; } public function offsetExists($name) { return isset($this->data[$name]); } public function offsetUnset($name) { unset($this->data[$name]); } } function my_array_modify($data) { //note - not reference &$data $data['b'] = 2; } $regularArray = array(); $arrayObject = new MyArrayObject(); $regularArray['a'] = 1; $arrayObject['a'] = 1; my_array_modify($regularArray); my_array_modify($arrayObject); var_dump($regularArray['b']); // PHP Notice: Undefined index: b var_dump($arrayObject['b']); // int(2) ---- Server IP: 116.193.170.20 Probable Submitter: 79.173.8.136 ---- Manual Page -- http://www.php.net/manual/en/class.arrayobject.php Edit -- https://master.php.net/note/edit/108687 Del: integrated -- https://master.php.net/note/delete/108687/integrated Del: useless -- https://master.php.net/note/delete/108687/useless Del: bad code -- https://master.php.net/note/delete/108687/bad+code Del: spam -- https://master.php.net/note/delete/108687/spam Del: non-english -- https://master.php.net/note/delete/108687/non-english Del: in docs -- https://master.php.net/note/delete/108687/in+docs Del: other reasons-- https://master.php.net/note/delete/108687 Reject -- https://master.php.net/note/reject/108687 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#188040) next »