PHP Type Hinting – Good practices

Let me introduce you “Type hinting”Type Hinting - PHP

Lets face it, PHP was thought and developed as a weakly typed programming language. But with more and more Object Oriented Approaches and structures introduced into PHP it needs to comply to certain rules. Those rules have been introduced in version 5 of PHP we have Type hinting which allows us to define the parameters’ type that a method or function expects which according to the documentation could be class or interface name, array or callable function or method.

How does it work?

Type hinting is quite straight forward approach. In the following example I’ll create a class that will use it:
[cc escaped=”true” lang=”php” line=”1″]class Product
{
private array $params;
private array $children;

public function __construct(array $params)
{
$this->params = $params;
}

public function addChildProduct(Product $product)
{
$this->children = $product;
}
 }[/cc]
In the example above we have two type hintings for the two methods we have defined, namely __construct & addChildProduct, this means that the code will expect corresponding variable types to be passed, e.g. “array” and object of type “Product”. If for some reason we pass different variable than expected the parser will throw a Fatal error similar to:
[cc escaped=”true” lang=”text”]”Catchable fatal error: Argument 1 passed to Product::__construct() must be an array, integer given, called in…”[/cc]
Many developers used to procedural programming or loose typed approach would complaint that this is unexpected and unwanted behaviour but in the world of Object Oriented applications this is important requirement for writing working, reliable and robust code that will provide necessary feedback at the entry point of a method opposed to an ambiguous error somewhere down in the code that would take time for debugging. The other solution  of the problem would be an IF-ELSE block to check for parameter type on every method which is first code duplication and second extra work for us and we are lazy, we don’t like extra work 🙂

Other benefits / IDEs

While type hinting is crucial for good code structure and compliments the contracts between the objects and their methods it provides another important benefit to the developers, code completion. IDEs like Eclipse and NetBeans are able to parse and index your code and libraries so when you start typing a class or method it gives you possible options that match the code you’ve written. Further more the IDE is capable of showing an error or warning if you’ve passed a mistaken parameter type.

This functionality could be extended even further with adding a phpDocumentator tag like so:
[cc escaped=”true” lang=”php”]/**
* Method documentation
*
* @param Product $product
* @return void
**/
public function addChildProduct(Product $product)
{
$this->children = $product;
}[/cc]
This comment will tell the IDE that our method expects an object of type Product and doesn’t return value. For our example code this doesn’t look as a significant feature but imagine that you work on a project with thousands lines of code finding such small tips in the code would almost impossible or at least very time consuming so leave the IDE to do its job and you could enjoy the coding.

Issues with scalar variables

Type hinting is great feature and in PHP that is even greater because it uses hybrid implementation, e.g. allows you to specify parameter type or leave it blank for any type. But there is a problem with the entire approach, scalar or primitive variables cannot be type hinted. Variables like int, string, float can’t be checked in the method declaration, you’ll get Fatal error. There are few proposal for solving this issue:

  • Strict type hinting
  • Unenforced type hinting
  • Casting weak type hinting
  • Strict weak type hinting
  • Boxing based type hinting

Those approaches are similar in some aspects but this is not a part of this post so if you’d like to see possible implementations and example functionality you can check this blog post which gives good insight for each of the approaches.

It's only fair to share...Buffer this pageShare on Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn0Share on Reddit0Pin on Pinterest0Email this to someone
About

Just a guy with strong interest in PHP and Web technologies

Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*