note 119217 added to class.arrayaccess

From: Date: Wed, 20 Apr 2016 22:22:33 +0000
Subject: note 119217 added to class.arrayaccess
Groups: php.notes 
Request: Send a blank email to php-notes+get-205290@lists.php.net to get a copy of this message
A class that implements ArrayAccess will not work with array_push For example: <?php class TestArrayAccess implements ArrayAccess { private $container = array(); // ArrayAccess methods } $obj = new TestArrayAccess(); array_push($obj, 'Hello World!'); // Nothing will be added ?> One way of being able to use array_push would be by adding a toArray() method (note the return value is a reference). <?php class TestArrayAccess implements ArrayAccess { private $container = array(); // ArrayAccess methods public function &toArray() { return $this->container; } } $obj = new TestArrayAccess(); array_push($obj->toArray(), 'Hello World!'); // Will now be added to array ?> ---- Server IP: 72.52.91.14 Probable Submitter: 75.159.57.97 ---- Manual Page -- http://php.net/manual/en/class.arrayaccess.php Edit -- https://master.php.net/note/edit/119217 Del: integrated -- https://master.php.net/note/delete/119217/integrated Del: useless -- https://master.php.net/note/delete/119217/useless Del: bad code -- https://master.php.net/note/delete/119217/bad+code Del: spam -- https://master.php.net/note/delete/119217/spam Del: non-english -- https://master.php.net/note/delete/119217/non-english Del: in docs -- https://master.php.net/note/delete/119217/in+docs Del: other reasons-- https://master.php.net/note/delete/119217 Reject -- https://master.php.net/note/reject/119217 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205290) next »