Is it possible to implement an array-like object in PHP?

I want to implement an object that behaves like an array. It should be used as follows:

$var = new CustomCollection(retrieveFromWebService());
echo $var[0]; // legal
$var[0] = 'a'; // illegal

Can this be done in PHP using magic methods or another mechanism?

+3
source share
2 answers
+4
source

I think ArrayAccess is what you are looking for.

+1
source

All Articles