Is it possible to implement an array-like object in PHP?
I want to implement an object that behaves like 开发者_C百科an array. It should be used in this way:
$var = new CustomCollection(retrieveFromWebService());
echo $var[0]; // legal
$var[0] = 'a'; // illegal
Can this be done in PHP, using magic methods or another mechanism?
Your CustomCollection
class will need to implement the built-in ArrayAccess
interface.
See also: http://code.google.com/p/phpraise/source/browse/trunk/phpraise/core/collection/RaiseCollection.php
I think ArrayAccess is what you are looking for.
精彩评论