Building Scalable Systems with the Open-Closed Principle

Andrei Birta
3 min readFeb 7, 2023

--

Foto:pixabay.com

The Open-Closed Principle (OCP) is one of the five principles of SOLID, a set of principles for writing maintainable and scalable software. The OCP states that a software module or class should be open for extension but closed for modification. This means that the module or class should be designed in such a way that it can be extended to add new functionality, but its existing code should not be modified.

In practice, this principle is often implemented by using inheritance and polymorphism. A class should be designed so that it can be subclassed and clients of the class should be able to use objects of the subclass in place of objects of the superclass. This allows new functionality to be added by creating a new subclass, rather than modifying the existing class.

Here’s an example of how the OCP might be implemented in PHP:

class Shape {
public function area() {
// code to calculate the area of the shape
}
}

class Circle extends Shape {
private $radius;

public function __construct($radius) {
$this->radius = $radius;
}

public function area() {
return pi() * pow($this->radius, 2);
}
}

class Rectangle extends Shape {
private $width;
private $height;

public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}

public function area() {
return $this->width * $this->height;
}
}

In the above example, the Shape class defines an area() method which is used to calculate the area of any shape. The Circle and Rectangle classes both inherit from the Shape class and they implement their own version of the area() method. The developers of this code can use the Shape class and its subclasses Circle and Rectangle in the same way, to calculate area of these shapes. And if in future we want to add new shape, then we just need to create a new class for that shape and we don't need to change the existing code.

In this way, the OCP helps to ensure that a class is flexible and can be extended to meet the changing needs of the system, while minimizing the risk of introducing bugs or breaking existing code. It also makes it easier to understand and maintain the codebase over time, by clearly separating the extension points from the parts of the code that are more likely to change.

It’s important to note that OCP is not just about the inheritance, it can also be implemented using other techniques such as using interfaces and composition. While the example above uses class inheritance, you can also define an interface or abstract class which other classes will implement. It’s also important to note that adherence to the OCP is not always easy and it may lead to over-engineering and unnecessary complexity if not handled properly.

Get more valuable insights and tips by following me!
Also check my list of articles:
- Basics in PHP
- Different type of architecture
- PHP Interesting Stuff
- Tech Stuff
- SOLID Principle

--

--