Skip to content

Instantly share code, notes, and snippets.

@lennartvdd
Created October 15, 2014 09:01
Show Gist options
  • Save lennartvdd/678412b858bbc204e514 to your computer and use it in GitHub Desktop.
Save lennartvdd/678412b858bbc204e514 to your computer and use it in GitHub Desktop.
An example of implementation of single responsibility within PHP
<?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