note 119674 added to class.arrayaccess

From: Date: Sun, 31 Jul 2016 11:07:39 +0000
Subject: note 119674 added to class.arrayaccess
Groups: php.notes 
Request: Send a blank email to php-notes+get-206138@lists.php.net to get a copy of this message
You can use the array functions on a object of a class that implements ArrayAccess using the __invoke magic method in this way: <?php class ArrayVar implements ArrayAccess { private $data = []; public function __invoke() { return $this->data; } } ?> Now you can use it in this way: <?php $arrayar = new ArrayVar(); $arrayar['one'] = 'primer'; $arrayar['two'] = 'segon'; $arrayar['three'] = 'tercer'; $keys = array_keys($arrayar()); var_dump($keys); // array (size=3) // 0 => string 'one' // 1 => string 'two' // 2 => string 'three' $diff = array_diff($arrayar(), [ 'two' => 'segon']); var_dump($diff); // array (size=2) // 'one' => string 'primer' // 'three' => string 'tercer' ?> ---- Server IP: 72.52.91.14 Probable Submitter: 185.38.128.95 ---- Manual Page -- http://php.net/manual/en/class.arrayaccess.php Edit -- https://master.php.net/note/edit/119674 Del: integrated -- https://master.php.net/note/delete/119674/integrated Del: useless -- https://master.php.net/note/delete/119674/useless Del: bad code -- https://master.php.net/note/delete/119674/bad+code Del: spam -- https://master.php.net/note/delete/119674/spam Del: non-english -- https://master.php.net/note/delete/119674/non-english Del: in docs -- https://master.php.net/note/delete/119674/in+docs Del: other reasons-- https://master.php.net/note/delete/119674 Reject -- https://master.php.net/note/reject/119674 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#206138) next »