views:

135

answers:

2

Hi,

I would like to make a class in PHP such as it would be searchable with PHP native method array_search. Currently my class implements IteratorAggregate and Countable, which allows me to do foreach on it.

There is a couple of other SPL interfaces (SeekableIterator, ArrayAccess, etc...) which seems to maybe fit, but I would like to know if someone has already done this and which interface is suitable for what I want to do.

Thank you

+1  A: 

Check out FilterIterator. You implement your own class inheriting from it then do a compare in the accept method implementation in your class.

Kevin Peno
A: 

Make your class extend ArrayObject, if possible. If that's not possible, try implementing each of the interfaces that ArrayObject implements, one-by-one based on this manual to see which interface you really need to use for your object.

Lukman
Or use the ArrayAccess interface to maintain type integrity.
Kevin Peno