3

I want to implement an object that behaves like 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?

1

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to "partially implement" an interface?
nope. An interface requires the class to implement all of its methods unless the implementation is an interface or abstract class.
1

I think ArrayAccess is what you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.