How To Use Open Closed Principle in PHP/Laravel
Tags: opencloseprincipal, solidprincipal, cleancode, laravel
Published: 2023-08-11 05:20:58

In this tutorial, I’ll show you in a very simple way with an example, how you can use OCP(Open Closed Principle) in PHP/Laravel. let’s start walking…

> At first, let me explain what is Open closed Principle

Open Closed principle is the 2nd principle of the SOLID principle. In SOLID O is stand for Open Closed principle. Open Closed principle means A class/method should be open for extension but closed for modification, which means you can change behavior without modifying source code. You shouldn’t touch the root/main code. The main idea of this principle is to keep existing code from breaking when you implement new features, which means extending functionality by adding new code instead of changing existing code. In the programmatical word “Separate extensible behavior behind an interface, and flip the dependencies”, that means separate the behaviors, so the system can easily be extended, but never broken.

**Goal: Get to a point where you can never break the core of your system.**

> Let me give you some real-life example

At first, you need to imagine or think about where you actually break or violate the OCP(Open Closed principle) in real-life project. Imagine you have an eCommerce project where you have multiple payment method options and you may integrate more payment method in future in time to time and in this point there are chances to break OCP(Open Closed principle). Now drive into the deep. Look at the picture billow.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdmslxeyuq28yqdth1yk.png)

You can see in pay method there are two payment method credit card and PayPal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ff1ewkh7hamcykas5nqt.png)

And in payment class you also see two method for two different payment methods. And still now you are in safe zone. you are not breaking OCP(Open Closed Principle) but if you want to add another payment method like the picture below and now you are breaking OCP(Open Closed Principle) as you are modify the existing pay method, not extending.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xme1u0uyij5ghqay32en.png)

You also breaking OCP(Open Closed Principle) in payment class as you are modify the payment class instead of extending like the picture below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lue7t9cui1f5yuiunyf.png)

Now the question, how you can write code without breaking the OCP(Open Closed Principle). How you can extend code instead of modifying the existing code. How you can add functionality without modifying or touching the exiting code. now I’ll show you How. we solve the problem with “Factory Design Pattern”. Let’s solve the OCP violation! See the picture carefully given below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddjmtx6q40fefj45ns6j.png)

In the picture above you can see PayableInterface with pay method and three different payment method classes like CreditCardPayment, PaypalPayment, WirePayment. Every class implements the PayableInterface & uses the pay method of PayableInterface.

> N.B. If any class implements an interface then the class must use all the methods of that interface.

Now we need to create a Payment factory class where we can initialize our payment methods in the initializePayment method like the picture below. if we need to add more payment methods we can add easily without modifying the existing code like before. we just need to initialize the payment method from our Payment factory class & create a new payment method class by implementing our PayableInterface. that’s it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25yx1m49d8oey8q7kkv8.png)

Now you can just call the pay method by passing only the payment method type in the payment factory class via initializePayment method like the picture below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fnoh69np9ntepjx06j4.png)

In conclusion, the Open Closed principle is very simple to follow, it helps to add new features or functionality by extending existing code instead of modifying existing code. To follow the OCP you just need to be concerned about is, will I modify my code for adding new features or functionality in the future.

For more details of **SOLID** principle, you may have a look at the link given below. Please feel free to leave a comment if you have any feedback, questions or want me to write about another PHP/Laravel related topic.

https://github.com/mohasinhossain/SOLID-Principles-in-PHP