Exceptions and Generators Flashcards

1
Q

Describe PHP’s exception model.

A

Code can be surrounded in “try” blocks, for catching exceptions. Each try must have at least one corresponding catch or finally block. The thrown object must be an instance of the Exception class or a subclass of Exception, or a fatal error will result.

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

What happens when an extension is not caught?

A

A PHP fatal error will be issued, unless a handler has been defined with set_exception_handler().

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

What’s a finally block?

A

It may be specified after or instead of catch blocks. Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.

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

Can exceptions be cloned?

A

No. Attempting to clone an Exception will result in a fatal E_ERROR error.

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

What’s one thing you should do if you extend the built-in Exceptions class and re-define the constructor?

A

It’s recommended that you also call parent::__construct() to ensure all available data has been properly assigned.

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

What are generators?

A

Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.

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

A generator is the same as a normal function, except…

A

…instead of returning once, a generator can yield as many times as it needs in order to provide the values to be iterated over.

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

Can a generator return a value?

A

No. Doing so will result in a compile error.

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

Is an empty return statement valid syntax within a generator?

A

Yes. It will terminate the generator.

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

What does a generator function return when called?

A

An object that can be iterated over. When you iterate over that object (for instance, via a foreach loop), PHP will call the generator function each time it needs a value, then saves the state of the generator when the generator yields a value so that it can be resumed when the next value is required.

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

A yield statement looks like a return statement, except that…

A

…instead of stopping execution of the function and returning, yield instead provides a value to the code looping over the generator and pauses execution of the generator function.

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

What must you do if you use yield in an expression context (for example, on the right hand side of an assignment)?

A

You must surround the yield statement with parentheses.

$data = (yield $value); //valid
$data = yield $value; //not valid
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you yield a key/value pair?

A

Yes.

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

How can you yield a NULL value?

A

Call yield without an argument to yield a NULL value with an automatic key.

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

Can generator functions yield values by reference as well as by value?

A

Yes. This is done in the same way as returning references from functions: by prepending an ampersand to the function name.

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

What happens when a generator function is called for the first time?

A

An object of the internal Generator class is returned. This object implements the Iterator interface in much the same way as a forward-only iterator object would.

17
Q

What’s the primary advantage of generators?

A

Their simplicity. Much less boilerplate code has to be written compared to implementing an Iterator class, and the code is generally much more readable.

18
Q

Can generators be rewound once iteration has started?

A

No. Generators are forward-only iterators. This also means that the same generator can’t be iterated over multiple times: the generator will either need to be rebuilt by calling the generator function again, or cloned via the clone keyword.