note 112935 added to class.arrayaccess

From: Date: Thu, 08 Aug 2013 17:49:13 +0000
Subject: note 112935 added to class.arrayaccess
Groups: php.notes 
Request: Send a blank email to php-notes+get-195916@lists.php.net to get a copy of this message
Just to inform: the increment and decrement operators (both pre and post versions) does NOT changes a element of your ArrayAccess object, since the setOffset() seems to never get called in this situation. Look at the example below: <?php class A implements ArrayAccess { public $a = array('foo' => 1); public function offsetExists($offset) {} public function offsetGet($offset) { return $this->a[$offset]; } public function offsetSet($offset, $value) { $this->a[$offset] = $value; } public function offsetUnset($offset) {} } $a = new A; // these doesn't work var_dump($a['foo']++); // return: 1 expected: 1 var_dump($a['foo']); // return: 1 expected: 2 not ok var_dump(++$a['foo']); // return: 2 expected: 2 var_dump($a['foo']); // return: 1 expected: 2 not ok // these does work var_dump($a['foo']+=1); var_dump($a['foo']); // return 2, as expected var_dump($a['foo']=$a['foo']+1); var_dump($a['foo']); // return 3, as expected ?> ---- Server IP: 69.147.83.199 Probable Submitter: 200.246.145.131 ---- Manual Page -- http://php.net/manual/en/class.arrayaccess.php Edit -- https://master.php.net/note/edit/112935 Del: integrated -- https://master.php.net/note/delete/112935/integrated Del: useless -- https://master.php.net/note/delete/112935/useless Del: bad code -- https://master.php.net/note/delete/112935/bad+code Del: spam -- https://master.php.net/note/delete/112935/spam Del: non-english -- https://master.php.net/note/delete/112935/non-english Del: in docs -- https://master.php.net/note/delete/112935/in+docs Del: other reasons-- https://master.php.net/note/delete/112935 Reject -- https://master.php.net/note/reject/112935 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195916) next »