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

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

PHP-Coding-Practices.com: Composing Methods: Introduce Explaining Variable

DevShed: PHP Object Oriented Programming using LSP

NetTuts.com: How to Write Testable and Maintainable Code in PHP

Freek Lijten's Blog: SOLID - The O is for Open Closed Principle


Community Events

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


library performance interview release series opinion composer framework podcast community api symfony2 php7 version laravel conference extension introduction voicesoftheelephpant example

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