SOLID: Fundamental Principles of Software Development using PHP (2)

Igieborelvis
3 min readJul 27, 2022

--

This is a continuation of our implementation of SOLID principles using PHP. I encourage you to go through the first part where we implemented the first principle, Single-Responsibility Principle.

In this episode we shall implement the second principle, Open and Closed Principle. At the end of the whole series, we shall be writing real application with clean. Please read it to the end, drop your questions incase if there is any part that needs clarity or you can send me an email igieborelvis@gmail.com. Also don’t forget to share. Thank you.

SOLID stands for:

S — Single-Responsibility Principle

O — Open-Closed Principle

L — Liskov Substitution Principle

I — Interface Segregation Principle

D — Dependency Inversion Principle

The Open-Closed principle states that Objects or entities should be open for extension but closed for modification.

In the first part, we created an app that collects shapes, calculate their areas and returns their sum. Assuming, we have other shapes, we will have to modify the areasumcalculator class, which is against our principle.

Our aim here is to all a class or entity to be extended but not modified.

To solve this, we create an interface that all shapes must follow.

interface ShapeArea{

public function area();

}

We will create an exception for shapes that do not follow the interface

class InvalideShapeException extends Exception

{

public $message;

function __construct($mess = ‘Invalid shape’)

{

$this->message = $mess;

//return ‘This shape is invalid’;

}

public function getMyMessage()

{

return $this->message;

}

}

class Square implements ShapeArea

{

public $length;

function __construct($length)

{

$this->length = $length;

}

public function area()

{

$area = $this->length * $this->length;

return $area;

}

}

class Circle implements ShapeArea{

public $radius;

function __construct($radius)

{

$this->radius = $radius;

}

public function area()

{

$area = pi() * ($this->radius * $this->radius);

return $area;

}

}

To test our exception, lets create another shape that doesn’t implement shapeArea interface

class Triangle{

public $breadth;

public $height;

function __construct($breadth,$height)

{

$this->breadth = $breadth;

$this->height = $height;

}

public function myArea()

{

$area = 1/2 * ($this->breadth * $this->height);

return $area;

}

}

create the class that calculates the sum

class AreaSumCalculator{

protected $shapes;

public function __construct($shapes = [])

{

$this->shapes = $shapes;

}

public function sum()

{

$area = [];

foreach($this->shapes as $shape)

{

if(is_a($shape, ‘ShapeArea’))

{

$area[] = $shape->area();

}

else{

throw new InvalideShapeException;

}

}

return array_sum($area);

}

}

output the calculated sum

class AreaSumCalculatorOutput{

public $areasumcalculator;

public function __construct(AreaSumCalculator $areasumcalculator)

{

$this->areasumcalculator = $areasumcalculator;

}

public function jsonOutput()

{

$data = [

‘sum’ => $this->areasumcalculator->sum(),

];

return json_encode($data);

}

}

// create shapes or objects (instance of classes)

$circle = new Circle(10);

$square = new Square(5);

$triangle1 = new Triangle(10,23);

$triangle = new Triangle(1,23);

// create a shape variable to accept an array of the area of shapes (object created)

$shapes1 = [$circle, $square];

$shapes2 = [$triangle1, $triangle];

// calculate the sum of the areas of the shapes

try{

$area_sum_calculator = new AreaSumCalculator($shapes1);

// answer :

/* {

“sum”: 339.1592653589793

}

*/

// output the sum

$area_sum_output = new AreaSumCalculatorOutput($area_sum_calculator);

// call the output

$json_output = $area_sum_output->jsonOutput();

echo $json_output .’<br>’;

}catch(InvalideShapeException $e)

{

echo “Caught my exception\n”;

echo $e->getMyMessage();

}

// test the triangular shape

try{

$area_sum_calculator = new AreaSumCalculator($shapes2);

// answer :

// Caught my exception Invalid shape

// output the sum

$area_sum_output = new AreaSumCalculatorOutput($area_sum_calculator);

// call the output

$json_output = $area_sum_output->jsonOutput();

echo $json_output;

}catch(InvalideShapeException $e)

{

echo “Caught my exception\n”;

echo $e->getMyMessage();

}

I hope you understand. Like we said, you can drop your questions through the comment box or send me a mail with email above. Thanks for following.

--

--