News Feed
Sections




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

Michael Nitschinger:
A Journey on Avoiding Nulls in PHP
February 20, 2013 @ 12:17:39

Michael Nitschinger has written up a post looking at avoiding nulls in your applications in favor of a better kind of value handling - the introduction of "Optional" handling.

While every developer has kind of accepted their existence, they are suddenly there when we'd desperately need them to not show up. How often did you writeif($obj === null) in your PHP code? Can't there be a better, more elegant and fault-tolerant solution to the problem?

His solution is to create a PHP version of this "Optional" functionality (via an abstract class) that allows some evaluation of the returned value from method calls on the object. Methods like "isPresent", "getOrElse", "of" and "fromNullable" make it easier to work with null values instead of just the triple-equals checking. He includes not only the code for the classes you'll need to implement it but examples of it in use - an "Optional" abstract class and two child classes, "Present" and "Absent".

0 comments voice your opinion now!
avoid null return value optional absent present evaluation tutorial


blog comments powered by Disqus

Similar Posts

DZone.com: Video: PHP 5.3.1, MongoDB and Lithium on Windows 7 / IIS 7

Benjamin Eberlei's Blog: Testing Database Locks with PHPUnit and Gearman

Daniel Krook's Blog: Migrating PHP applications to DB2

DevShed: Sanitizing Input with PHP

Matthew Weier O'Phinney's Blog: Code Generation with Zend\CodeGenerator


Community Events





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


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

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