News Feed
Sections




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

Lars Strojny's Blog:
Antipattern the verbose constructor
July 31, 2008 @ 10:29:14

In this new post from Lars Strojny, there's a discussion of an "antipattern" - using the constructor for more than it was intended, the "verbose constructor".

Constructors are often used to shortcut dependency injection and parameter passing on instantiation. This is a valid practice and often leads to shorter code. [...] Instead of creating a new instance of "Money" and calling three setter, everything can be done compactly in the constructor. [...] So for the money object this works pretty well. The code is easy to read, but wait, the first argument can be grasped easily, the second too, but the third? It is not too obvious that it is a divisor is passed.

He compares three different ways to get the data into the class - the already-mentioned parameters in the constructor, passing an array into the constructor and using full getters/setters to push the data into the right places (with fluent interfaces even!).

1 comment voice your opinion now!
antipattern verbose constructor array getter setter


blog comments powered by Disqus

Similar Posts

Ben Scholzen's Blog: Writing powerful and easy config files with PHP-arrays

ProDevTips: Sorting 2D-arrays in PHP - anectodes and reflections

Maarten Balliauw's Blog: LINQ for PHP (Language Integrated Query for PHP)

Zend Developer Zone: The Standard PHP Library (SPL)

Felix Geisendorfer's Blog: Cake 1.2’s Set class eats nested arrays for breakfast!


Community Events





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


api tips language list application threedevsandamaybe developer release development framework series introduction deployment community laravel zendserver podcast code conference interview

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