[ Team LiB ] Previous Section Next Section

Workshop

Quiz

1:

What keyword would you use to define a class constant?

2:

A static property cannot be changed at runtime. True or false?

3:

Can a static method access normal object properties using the $this pseudo variable?

4:

What built-in method could you implement to catch an attempt to access a nonexistent property?

5:

How would you prevent a method being overridden?

6:

What built-in method will be called when an object is about to be destroyed?

7:

What keyword would you use to send an exception to client code?

8:

What clause would you define to handle a specific exception in client code?

9:

How would you instantiate an abstract class?


Answers

A1:

You would define a class constant with the const keyword:

const DJ="John Peel"

A2:

False. A static property can be changed throughout script operation. Statics are properties that are set at class rather than object level.

A3:

No. Static methods have no direct access to object properties because they do not exist in object context.

A4:

You can implement the __get() method to catch attempts to read nonexistent properties.

A5:

You can prevent a method from being overridden by declaring it final:

final function getDJ() {
  return "john peel";
}

A6:

The __destruct() method is called when an object is about to be destroyed.

A7:

You can send an exception to calling code with the throw keyword:

throw new WrongDjException("not John Peel");

A8:

You can handle a specific exception using a catch clause:

try {
  getRightDJ();
} catch ( WrongDjException $e ) {
  print "Sorry an error occurred";
}

A9:

This is a trick question. You cannot instantiate an abstract class; you must subclass it and instantiate a concrete child class.


    [ Team LiB ] Previous Section Next Section