(For more background, check out Wikipedia articles about Data Type and Strong and Weak Typing. This article’s code examples will focus more on method type hinting and not casting of types.
As of PHP 5.5, the following types can be hinted: classes, interfaces, arrays, and callable. Resources, Traits and Scalar types are not currently allowed at this time. When hinting objects, one important thing to remember is that any class or interface that makes up the entire object can be hinted. This means child classes can still satisfy a parent class hint.
As a Confident Coder, we know that we want our methods to be doing the least amount of work as possible. I’d rather not have to handle converting data types.
The following style illustrated in this example might be familiar to you:
function doSomething($value)
{
$value = (array) $value;
// ...
Since it was unclear what data type the parameter will be, it was cast to an array. Other examples might use functions like is_array()
to determine if this parameter is of the required type.
If I document my function (and now have a type-hinted signature), it will be the calling code’s responsibility to provide my function with the proper variable type.
Note how the previous example can be refactored to require the proper data type:
function doSomething(array $value)
This will now require the incoming variable to be type array.
Often, Confident Coders find themselves building modular, reusable code as a library or the base of an application. Usually, this code can then be implemented or extended to create other applications. Because of this, the original programmer, other programmers on the team, or even the entire user base may be using the code and calling public methods. When you introduce this much flexibility and utility, it is even more important to impose standards and to be precise. This is an example where type-hinting in PHP really shines.
Imagine a code base where programmers can send any of their objects into your method as a parameter. However, since your method requires a method of the parameter object’s to be called every single time, you can define an interface for those objects to implement. Then, using the interface as the type hint in the function signature, it can be guaranteed that the methods exist.
This interface and class method help demonstrate this:
class MyProcessorClass
{
function process(MyPluginInterface $yourPlugin)
{
$yourPlugin->doSomething();
}
}
interface MyPluginInterface
{
public function doSomething();
}
Since the process()
method will always be calling the doSomething()
method of any object passed as a parameter, the interface defines the doSomething()
method. This means that any class implementing this interface must have that method. And since the process()
method has type-hinted its parameter to be that interface, we can be assured that any object, no matter what base class it is, will at least have that callable method because it will be forced to implement the interface.
Confident Coders know and appreciate the balance between the ease-of-use of our beloved loosely-typed language and the accuracy and standards enforced by strong typing. It is incredibly important to understand the unique value that type-hinting inside a loosely typed language can add to your programming. Learn to balance and embrace the flexibility of PHP with the benefits that type-hinting can add.
This entry is republished from the original columns included years ago in the PHP Architect magazine. I really recommend purchasing the magazine to get timely articles, columns and PHP news.