Created
October 15, 2014 09:01
-
-
Save lennartvdd/678412b858bbc204e514 to your computer and use it in GitHub Desktop.
An example of implementation of single responsibility within PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Class Foo | |
{ | |
/** | |
* not a method with a single responsibility | |
*/ | |
public function __construct() | |
{ | |
//imagine there is a lot of procedural code here performing the following logic: | |
//read request | |
//configure the object | |
//prepare session | |
} | |
//In order to 'fix' this you could instead implement the same logic in a more single | |
//responsibility manner, by moving all the logic to their own methods. | |
//This will make the same code a lot more readable, testable and flexible for future changes. | |
/** | |
* wrapper method for single responsibility methods | |
*/ | |
public function __construct() | |
{ | |
$this->readRequest(); | |
$this->init(); | |
$this->beginSession(); | |
} | |
private function readRequest() | |
{ | |
} | |
private function init() | |
{ | |
} | |
private function beginSession() | |
{ | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment