note 107079 added to class.arrayobject

From: Date: Thu, 05 Jan 2012 16:24:37 +0000
Subject: note 107079 added to class.arrayobject
Groups: php.notes 
Request: Send a blank email to php-notes+get-185374@lists.php.net to get a copy of this message
As you know ArrayObject is not an array so you can't use the built in array functions. Here's a trick around that: Extend the ArrayObject class with your own and implement this magic method: <?php public function __call($func, $argv) { if (!is_callable($func) || substr($func, 0, 6) !== 'array_') { throw new BadMethodCallException(__CLASS__.'->'.$func); } return call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv)); } ?> Now you can do this with any array_* function: <?php $yourObject->array_keys(); ?> - Don't forget to ommit the first parameter - it's automatic! Note: You might want to write your own functions if you're working with large sets of data. ---- Server IP: 69.147.83.197 Probable Submitter: 31.46.123.52 ---- Manual Page -- http://www.php.net/manual/en/class.arrayobject.php Edit -- https://master.php.net/note/edit/107079 Del: integrated -- https://master.php.net/note/delete/107079/integrated Del: useless -- https://master.php.net/note/delete/107079/useless Del: bad code -- https://master.php.net/note/delete/107079/bad+code Del: spam -- https://master.php.net/note/delete/107079/spam Del: non-english -- https://master.php.net/note/delete/107079/non-english Del: in docs -- https://master.php.net/note/delete/107079/in+docs Del: other reasons-- https://master.php.net/note/delete/107079 Reject -- https://master.php.net/note/reject/107079 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185374) next »