News Feed
Sections




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

PHPMaster.com:
Constructors and the Myth of Breaking the Liskov Substitution Principle
October 08, 2012 @ 11:53:13

On PHPMaster.com there's a new post in a series looking at the SOLID design principles in PHP development. In this new tutorial they try to dispel the myth that constructors break the Liskov Substitution Principle ("L" in "SOLID").

Rants aside, the morale of the story can be boiled down to the following: "Object construction is not part of the contract honored by its implementers". It's easier to grasp concepts by example rather than reading dull theory, so in this article I'll be demonstrating from a practical standpoint how the implementation of different constructors down the same hierarchy isn't a violation of the Liskov Substitution Principle, which is a reason why you shouldn't fall into the temptation of tainting your interfaces with constructors.

He illustrates the point with a simple PDO class that implements a "DatabaseAdapterInterface" interface that defines its own constructor that follows the defaults of the PDO extension. He goes on and changes the constructor for the class a bit to take in an array of config options rather than the DSN/User/Password combo. Inside of this constructor, those values are then taken and pushed into PDO to create the connection. He also suggests one other solution - the injection of a connection object ("ConnectionDefinition") into the constructor instead of the configuration directly.

0 comments voice your opinion now!
solid development liskov substitution principle tutorial constructor


blog comments powered by Disqus

Similar Posts

Gergely Hodicska's Blog: What's new in PHP 5.3 - part 1: namespaces

Tomas Petricek's Blog: Writing Silverlight applications in PHP

SitePoint PHP Blog: Build Virtual Machines Easily with PuPHPet Part 2

SpinDrop.us: Using Zend Search Lucene in a symfony app

Rob Zienert's Blog: Zend_Form Decorators and Composite Elements


Community Events





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


install developer symfony2 list podcast code api community framework unittest testing interview introduction opinion release language refactor series laravel threedevsandamaybe

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