开发者

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

开发者 https://www.devze.com 2023-02-16 13:49 出处:网络
I want to implement an object that behaves like 开发者_C百科an array. It should be used in this way:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消