Make an object searchable with array_search in PHP
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
Check out FilterIterator. You implement your own class inheriting from it then do a compare in the accept
method implementation in your class.
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.
精彩评论