Type Hinting in PHP
Since PHP 5 , php has started supporting type hinting like other OOP languages. Hence, you can enforce your functions to get an argument of particular object type. PHP will check if the particular argument is of that type, if not will raise an error. However it still doesn’t support type hinting for scalar data types like string and integers.
This is what php manual has to say about type hinting:
A very good example explaining type hinting:
Supporting Type Hinting is a great step which PHP has taken to move towards strongly typed languages like JAVA and making things more easy for developers from other language environments to adopt it as it is. Moreover the path PHP is going since adoption of design patterns, namespaces, closures, traits and other cool features from OOP languages like JAVA has changed the complete scenario today . This is a completely different topic to be discussed which i intend to write in some other blog post.
One of the great benefits of type hinting is that it provide a built-in documentation if the author doesn’t provide any documentation with his code . However there is still a controversy among developers for supporting scalar data types. The real fun of PHP is that it is a loosely-typed language and you don’t have to worry about the type conversions, write a little code and get it up running asap. With Type Hinting things are better, streamlined but i dont think anyone who has been in the php community for long will support type hinting for scalar data types. This how we PHP developers want it to be.