News Feed
Sections




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

Ralph Schindler's Blog:
PHP Constructor Best Practices And The Prototype Pattern
March 12, 2012 @ 11:26:10

In this new post Ralph Schindler takes a look at the Prototype design pattern and uses it to illustrate some best practices in using constructors in PHP.

If your knowledge of constructors ends with "the place where I put my object initialization code," read on. While this is mostly what a constructor is, the way a developer crafts their class constructor greatly impacts the initial API of a particular class/object; which ultimately affects usability and extensibility. After all, the constructor is the first impression a particular class can make.

He starts at ground level, reintroducing what a constructor is and what it should (and shouldn't) be used for. He talks about constructor overloading, constructor injection, dynamic class extension and using the Prototype pattern to create "an unlimited number of objects of a particular type, with dependencies in tact, each with slight variations." He gives an example with a "DbAdapter" class, showing dynamic class instantiation and how to, using the Prototype method, inject a DbAdapter object and have your class use that instead.

0 comments voice your opinion now!
constructor best practice prototype design pattern


blog comments powered by Disqus

Similar Posts

DevShed: Design Patterns and PHP 5

Community News: CakePHP 1.2 Release (and a New Site Design)

Tobias Schlitt's Blog: Reading recommendation: PHP Design Patterns

Stefan Mischook's Blog: Book Review: PHP 5 in Practice

Hasin Hayder's Blog: Did you know 'you can have cheese burger' could be a variable name?


Community Events





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


podcast series community library language mvc version voicesoftheelephpant tool symfony laravel framework package release introduction security composer update opinion interview

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