I am writing a class (Hash) that adds behaviour to array. The instantiated objects should be interchangeable with the primitive array, so I can pass it to methods that require arrays. Also I want to be able to cast the object to array
The test it should pass is here.
I thought implementing ArrayAccess would be enough, but it is not.
Or perhaps implementing a __toArray()
method, like toString()
for casting strings, but it also won't work.
edit:
$hash = new Hash(array(
'foo' => 'bar',
'bar' => 'barfoo',
));
function echoArray(array $array) {
print_r($array);
}
echoArray($hash);
// Catchable fatal error: Argument 1 passed to echoArray()
// must be of the type array, object given
print_r((array) $hash);
/*
Array
(
[*_values] => Array
(
[foo] => bar
[bar] => barfoo
)
)
*/
Is there a way of achieving that behaviour?
Thank you in advance.