Predefined Interfaces and Classes Flashcards
What are the predefined interfaces and classes?
Traversable Iterator IteratorAggregate ArrayAccess Serializable Closure Generator
(There are also the SPL interfaces and reserved classes.)
What is Traversable for?
It’s an interface to detect if a class is traversable using foreach.
Can Traversable be implemented alone?
No, it’s an abstract base class that must be implemented by either IteratorAggregate or Iterator.
What methods are in Traversable?
None. Its only purpose is to be the base interface for all traversable classes.
What is Iterator?
The interface for external iterators or objects that can be iterated themselves internally.
What does Iterator extend?
Traversable.
What are Iterator’s methods?
current, key, next, rewind, and valid
What does Iterator::key() return?
The key of the current element. Scalar on success, and NULL on failure (and issues an E_NOTICE).
What does Iterator::next() return?
Any return value is ignored.
What does Iterator::rewind() do?
It rewinds back to the first element of the iterator. This is the first method called when starting a foreach loop. It will NOT be executed AFTER foreach loops.
When is Iterator::valid() called?
After Iterator::rewind() and Iterator::next() to check if the current position is valid.
What does Iterator::valid() return?
The return value will be casted to boolean and then evaluated. Returns TRUE on success or FALSE on failure.
What is IteratorAggregate for?
It’s an interface to create an external iterator.
What does IteratorAggregate extend?
Traversable
What does IteratorAggregate::getIterator do?
It retrieves an external iterator.
What does IteratorAggregate::getIterator return?
An instance of an object implementing Iterator or Traversable. On failure, it throws an Exception.
What is the ArrayAccess interface?
It’s an interface to provide accessing objects as arrays.
What are the methods in ArrayAccess?
offsetExists, offsetGet, offsetSet, offsetUnset
When is ArrayAccess::offsetExists executed?
When using isset() or empty() on objects implementing ArrayAccess.
What does ArrayAccess::offsetExists return?
TRUE on success, FALSE on failure. The return value will be casted to boolean if non-boolean was returned.