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

Tibo Beijen's Blog: Explicit PHP6?

DevShed: PHP Object Oriented Programming using LSP

Anthony Ferrara: Beyond Object Oriented Programming

NetTuts.com: SOLID: Part 3 - Liskov Substitution & Interface Segregation Principles

PHPMaster.com: The Open/Closed Principle


Community Events





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


opinion library api interview symfony release community tips laravel series list update deployment install language introduction framework package zendserver podcast

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