News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Wojciech Sznapka:
Type Hinting is important
June 12, 2014 @ 09:41:51

In his latest post Wojciech Sznapka reminds us that type hinting is important in your PHP applications and can help provide more structure and better overall code practices.

One of my favorite PHP interview questions, is: what is Type Hinting and why it's important? Putting definition in one sentence, Type Hinting is a way to define type of parameter in function signature and it's a sine qua non to leverage polymorphism. [...] So given the fact, that Type Hinting is optional and we don't need to specify types of parameters passed to the method - why bother? Answer is easy: well prepared method signatures defines your model and are part of the "contract" that your code reveals to its consumers. It also prevents many silly errors and keeps codebase clean and coherent.

He talks about the best practices on using type hinting including where to put them (in an interface or base class or child class?) and some of the pros and cons of each. He also points out that some practices around type hinting, like overriding the hint and calling the method with unexpected/variable input, should be avoided (see the L in SOLID).

0 comments voice your opinion now!
typehint importance bestpractice liskov substitution principle solid

Link: http://blog.sznapka.pl/type-hinting-is-important

blog comments powered by Disqus

Similar Posts

DZone.com: SOLID for packag...err, namespaces

PHPEasy.co.uk: Tutorial: Type Hinting in PHP

Ralph Schindler's Blog: PHP: Environments, Libraries, and Applications - Oh My!

Seth May's Blog: The 5 Ws of Data Validation - Part 1

DevShed: Violating the Liskov Substitution Principle - PHP


Community Events





Don't see your event here?
Let us know!


framework introduction library package opinion series update symfony community laravel version interview language podcast security mvc voicesoftheelephpant tool composer release

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework