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 be used to advantage.)

Controlling How Functions Are Used

Type hinting can give a programmer more control over the way that a function is used. Suppose you derive a Canary class and a Lark class from the Bird class shown in Listing 11-1. You could pass either a canary or a lark to the function in Listing 11-2.

Listing 11-2. A function that uses type hinting to specify a Bird object
function doSomething(Bird $b){
   //do something
   $b->sing();
   //do something else
}

Even though the Bird class is an abstract class that cannot be instantiated, you can use it to type hint the argument to this function in exactly the same way that catch blocks are type hinted.

In Listing 11-2, type hinting prohibits passing anything but a bird to the function—passing any other object or a primitive will result in an error. In this way, a programmer can restrict the way that a function is used. With properly ordered catch blocks you used type hinting to catch specific kinds of exceptions. The doSomething function does the converse; it catches any kind of Bird. The ability to pass any kind of Bird to this function without knowing the specific kind beforehand, with the expectation that it will behave as it is supposed to behave, is known as polymorphism. The parent takes on the characteristics of the child.

As you are aware, PHP is a weakly-typed language. In the strictest sense, polymorphism requires a strongly-typed language such as Java or C++. In these languages, whenever a variable is declared or used as a function parameter, it is declared as a specific data type. In PHP, type hinting a parameter doesn't define the data type but merely filters for acceptable types. In terms of the code in Listing 11-2, Bird doesn't define the type of $b; it simply blocks out all other types. If this is the case, then $b is a variable like any other PHP variable, of no specific type. It is a variant that becomes a type through assignment. You don't in fact have a Bird class with the capability of performing the methods of whatever child class is passed. You have only the child class itself. Hence it is disputable whether PHP in fact supports polymorphism.

Regardless of whether PHP is truly polymorphic, the combination of type hinting and abstract methods is a powerful tool. The former guarantees a certain kind of object, and the latter guarantees the implementation of particular methods. For these reasons you can be sure that any object passed to the doSomething function will implement the sing method. The declaration of an abstract sing method ensures that you can't have a bird that doesn't sing and the type hint ensures that only a bird may be passed to this function.


Note:

Type hinting is optional in all situations except catch blocks. A variable's data type in a catch must be specified, and it must be an Exception or a class derived from Exception. Type hinting applies to objects only (although as of PHP 5.1, arrays can also be type hinted). Type-hinted code is also self-documenting because it makes the programmer's intentions explicit. (We'll discuss this topic in greater detail in Chapter 14.)


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.133