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:
The Single Responsibility Principle
November 22, 2012 @ 11:58:06

On PHPMaster.com today Alejandro Gervasio has a new tutorial posted about the Single Responsibility Principle - a guideline that states that each class should only have one "area of concern" and not try to do to much.

One of the most notorious consequences of this rational associative process is that, at some point, we effectively end up creating classes that do too much. The so-called "God class" is quite possibly the most extreme and coarse example of a structure that packages literally piles of unrelated operations behind the fence of the same API, but there are other subtle, more furtive situations where assigning of multiple roles to the same class are harder to track down. [...] What the principle attempts to promote is that classes must always be designed to expose only one area of concern and the set of operations they define and implement must be aimed at fulfilling that concern in particular and nothing else.

He starts off with a typical violation of the principle, showing a class that not only handles user data but also includes the functions to work with the database directly as well (insert/update/delete). He refactors this into a few much more manageable classes - a mapping class to handle the database interaction and a "User" class representative of a true user object.

0 comments voice your opinion now!
single responsibility principle srp class tutorial refactor


blog comments powered by Disqus

Similar Posts

Vidyut Luther\'s Blog: Where do you \"define\" your environment/global settings?

PHPRiot.com: Zend Framework 101: Zend_Registry

DZone.com: MySQL PHP search: four-part video tutorial series

Inviqa techPortal: Profiling PHP Applications with XHGui

DevShed: Migrating Class Code for a MIME Email to PHP 5


Community Events

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


release series interview podcast opinion laravel language unittest conference api voicesoftheelephpant library development wordpress introduction community framework psr7 extension laravel5

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