Type Hinting
As I have seen repeatedly throughout this tutorial, PHP takes a dynamic approach to typing and doesn't require us to declare types when I want to use variables or add parameters to functions. However, one place where PHP does give us some extra control of typing is in passing parameters to functions. Programmers now have the option of providing a type hintthis is a way to tell PHP that I expect a parameter to be of a certain class. However, I cannot do this with things that are not objects (for example, integers or strings).
<?php
function get_product_info(IProduct $in_product)
{
// etc.
}
?>
In the previous code snippet, the function expects its sole parameter to be of type IProduct. This means that any class that implements IProduct, such as my LocalProduct, NavigationPartnerProduct, or SuperBoatsProduct classes, is acceptable as a parameter to this function. If I wanted the function to only accept function classes that I have written in-house and inherit from Product, I could declare it as follows:
<?php
function get_product_info(Product $in_product)
{
// etc.
}
?>
If you try to pass a parameter that does not match the type hint, PHP gives you an error.
Fatal error: Argument 1 must be an object of class IProduct in c:\Inetpub\wwwroot\phpwebapps\src\hints on line 7