Voting

The Note You're Voting On

jordistc at gmail dot com
8 years ago
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'
?>

<< Back to user notes page

To Top