Polymorphism

In Chapter 10 you created a MySQLException class by inheriting from Exception. Type hinting allowed you to easily distinguish different kinds of exceptions and made it possible to have more than one catch block. However, when using type hinting, you also had to order the catch blocks carefully to make sure that the child preceded the parent. Specifically, MySQLException had to precede Exception because a catch block that catches the Exception class will also catch any derived class. Because it is derived from Exception, MySQLException can be caught by an Exception catch block. A parent class can stand in for its children, but a child cannot stand in for its parent. (This may look like a drawback, but you'll soon see how it can ...

Get Object-Oriented PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.