HomePHP Enforcing Object Types in PHP: Using the Type Hinting Feature in PHP 5
Enforcing Object Types in PHP: Using the Type Hinting Feature in PHP 5
Here we are again. Welcome to the last tutorial of the series “Enforcing object types in PHP.” As this article’s title suggests, this series introduces the basics of object type enforcement in PHP, covering some of the most common methods for checking types of objects in both PHP 4 and PHP 5 respectively.
As you’ll hopefully recall, in the previous article I explained the key points of using the “instanceof” operator available in PHP 5, in order to check the type of objects that eventually could be passed as input arguments to other PHP classes across the same Web application.
True to form, this operator is very useful in cases where filtering objects is a must, and of course when the flow of an application must be moved to different code blocks, in accordance with the type of object aggregated to a particular class. Additionally, you can use the “instanceof” operator for checking whether certain objects are implementers of a specific interface too. As you can see, possibilities for utilizing this operator in PHP 5 applications are certainly numerous.
Now, returning to the subject of this last tutorial, I’ll introduce another method for enforcing object types in PHP 5: the “Type Hinting” feature. It can also be used in conjunction with the “instanceof” operator that you learned about before, in order to develop PHP applications that implement thorough routines for filtering unwanted objects. Generally speaking, when you finish reading this article, you should be armed with a few more methods for forcing object types in PHP, in this way expanding your overall knowledge of object-oriented programming.
With the formalities out of the way, let’s get started.