Predefined Interfaces and Classes Flashcards

1
Q

What are the predefined interfaces and classes?

A
Traversable
Iterator
IteratorAggregate
ArrayAccess
Serializable
Closure
Generator

(There are also the SPL interfaces and reserved classes.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Traversable for?

A

It’s an interface to detect if a class is traversable using foreach.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can Traversable be implemented alone?

A

No, it’s an abstract base class that must be implemented by either IteratorAggregate or Iterator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What methods are in Traversable?

A

None. Its only purpose is to be the base interface for all traversable classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Iterator?

A

The interface for external iterators or objects that can be iterated themselves internally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does Iterator extend?

A

Traversable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are Iterator’s methods?

A

current, key, next, rewind, and valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does Iterator::key() return?

A

The key of the current element. Scalar on success, and NULL on failure (and issues an E_NOTICE).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does Iterator::next() return?

A

Any return value is ignored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does Iterator::rewind() do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When is Iterator::valid() called?

A

After Iterator::rewind() and Iterator::next() to check if the current position is valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does Iterator::valid() return?

A

The return value will be casted to boolean and then evaluated. Returns TRUE on success or FALSE on failure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is IteratorAggregate for?

A

It’s an interface to create an external iterator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does IteratorAggregate extend?

A

Traversable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does IteratorAggregate::getIterator do?

A

It retrieves an external iterator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does IteratorAggregate::getIterator return?

A

An instance of an object implementing Iterator or Traversable. On failure, it throws an Exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the ArrayAccess interface?

A

It’s an interface to provide accessing objects as arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the methods in ArrayAccess?

A

offsetExists, offsetGet, offsetSet, offsetUnset

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

When is ArrayAccess::offsetExists executed?

A

When using isset() or empty() on objects implementing ArrayAccess.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does ArrayAccess::offsetExists return?

A

TRUE on success, FALSE on failure. The return value will be casted to boolean if non-boolean was returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

When is ArrayAccess::offsetGet executed?

A

When checking if offset is empty().

22
Q

What does ArrayAccess::offsetGet return?

A

The value at the specified offset.

23
Q

What is Serializable for?

A

Custom serializing.

24
Q

What are the methods in Serializable?

A

serialize and unserialize

25
Q

Is the __destruct() method called after Serializable::serialize()?

A

No. This method acts as the destructor.

26
Q

What is the Closure class?

A

It is used to represent anonymous functions.

27
Q

What do anonymous functions yield?

A

Objects of type Closure.

28
Q

What are the methods in the Closure class?

A

__construct, bind, bindTo, and call.

29
Q

What is the purpose of the Closure::__construct() method?

A

It exists only to disallow instantiation of the Closure class.

30
Q

What is the return value of Closure::__construct()?

A

It has no return value; it simply emits an error of type E_RECOVERABLE_ERROR.

31
Q

What is Closure::bind()?

A

It duplicates a closure with a specific bound object and class scope. It’s a static version of Closure::bindTo().

32
Q

What are the parameters for Closure::bind()?

A
closure - the anonymous functions to bind
newthis - the object to which the given anonymous function should be bound, or NULL for the closure to be unbound.
newscope - the class scope to which associate the closure is to be associated, or 'static' to keep the current one. If an object is given, the type of the object will be used instead. This determines the visibility of protected and private methods of the bound object.
33
Q

What does Closure::bind() return?

A

A new Closure object or FALSE on failure.

34
Q

What does Closure::bindTo() do?

A

It duplicates the closure with a new bound object and class scope.

35
Q

What are the parameters to Closure::bindTo()?

A

newthis - the object to which the given anonymous function should be bound, or NULL for the closure to be unbound.

newscope - the class scope to which associate the closure is to be associated, or ‘static’ to keep the current one. If an object is given, the type of the object will be used instead. This determines the visibility of protected and private methods of the bound object.

36
Q

What does Closure::bindTo() return?

A

The newly created Closure object or FALSE on failure.

37
Q

What does Closure::call() do?

A

It calls the Closure with the given parameters and returns the result, with $this bound to the given object $to.

38
Q

What are the parameters to Closure::call()?

A

to

parameters

39
Q

Can Generator objects be instantiated via ‘new’?

A

No.

40
Q

What does Generator implement?

A

Iterator.

41
Q

What are the methods in Generator?

A

current, key, next, rewind, send, throw, valid, __wakeup

42
Q

What does Generator::send() do?

A

It sends the given value to the generator as the result of the current yield expression and resumes execution of the generator.

43
Q

What happens if the generator is not at a yield expression when Generator::send() is called?

A

It will first be let to advance to the first yield expression before sending the value.

44
Q

What are the parameters to Generator::send()?

A

value - value to send into the generator. This value will be the return value of the yield expression the generator is currently at.

45
Q

What does Generator::throw() do?

A

It throws an exception into the generator and resumes execution of the generator. The behavior will be the same as if the current yield expression was replaced with a throw $exception statement.

46
Q

What happens if the generator is already closed when the Generator::throw() method is invoked?

A

The exception will be thrown in the caller’s context instead.

47
Q

What does Generator::throw() return?

A

The yielded value.

48
Q

What does Generator::valid() do?

A

It checks if the iterator has been closed.

49
Q

What does Generator::valid() return?

A

FALSE if the iterator has been closed. Otherwise, TRUE.

50
Q

What does Generator::wakeup() do?

A

It’s a serialize callback that throws an exception, as generators can’t be serialized. It has no parameters. No value is returned.