note 119673 deleted from class.arrayobject by cmb
| From: | cmb@php.net | Date: | Sun, 31 Jul 2016 13:20:42 +0000 |
| Subject: | note 119673 deleted from class.arrayobject by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206139@lists.php.net to get a copy of this message | ||
Note Submitter: jordistc at gmail dot com
----
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 like this:
<?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'
?>