<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohasin Hossain</title>
    <description>The latest articles on DEV Community by Mohasin Hossain (@mohasindev).</description>
    <link>https://dev.to/mohasindev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F862281%2F12770550-9c24-4cf5-b384-6901a103870a.jpeg</url>
      <title>DEV Community: Mohasin Hossain</title>
      <link>https://dev.to/mohasindev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohasindev"/>
    <language>en</language>
    <item>
      <title>How To Use Open Closed Principle in PHP/Laravel</title>
      <dc:creator>Mohasin Hossain</dc:creator>
      <pubDate>Fri, 11 Aug 2023 05:20:58 +0000</pubDate>
      <link>https://dev.to/mohasindev/how-to-use-open-closed-principle-in-phplaravel-d80</link>
      <guid>https://dev.to/mohasindev/how-to-use-open-closed-principle-in-phplaravel-d80</guid>
      <description>&lt;p&gt;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…&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;At first, let me explain what is Open closed Principle&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goal: Get to a point where you can never break the core of your system.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let me give you some real-life example&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4nae1QXv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdmslxeyuq28yqdth1yk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4nae1QXv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdmslxeyuq28yqdth1yk.png" alt="Image description" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see in pay method there are two payment method credit card and PayPal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d0AdPd15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ff1ewkh7hamcykas5nqt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d0AdPd15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ff1ewkh7hamcykas5nqt.png" alt="Image description" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pD4aeyz8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xme1u0uyij5ghqay32en.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pD4aeyz8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xme1u0uyij5ghqay32en.png" alt="Image description" width="783" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NGiyFL64--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lue7t9cui1f5yuiunyf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NGiyFL64--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lue7t9cui1f5yuiunyf.png" alt="Image description" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KISItUz6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddjmtx6q40fefj45ns6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KISItUz6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddjmtx6q40fefj45ns6j.png" alt="Image description" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &amp;amp; uses the pay method of PayableInterface.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;N.B. If any class implements an interface then the class must use all the methods of that interface.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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 &amp;amp; create a new payment method class by implementing our PayableInterface. that’s it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uJ-5DiMh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25yx1m49d8oey8q7kkv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uJ-5DiMh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25yx1m49d8oey8q7kkv8.png" alt="Image description" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--klISaZXH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fnoh69np9ntepjx06j4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--klISaZXH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fnoh69np9ntepjx06j4.png" alt="Image description" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;For more details of &lt;strong&gt;SOLID&lt;/strong&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mohasinhossain/SOLID-Principles-in-PHP"&gt;https://github.com/mohasinhossain/SOLID-Principles-in-PHP&lt;/a&gt;&lt;/p&gt;

</description>
      <category>opencloseprincipal</category>
      <category>solidprincipal</category>
      <category>cleancode</category>
      <category>laravel</category>
    </item>
    <item>
      <title>How To Use Liskov Substitution Principle in PHP/Laravel</title>
      <dc:creator>Mohasin Hossain</dc:creator>
      <pubDate>Thu, 03 Aug 2023 07:01:08 +0000</pubDate>
      <link>https://dev.to/mohasindev/how-to-use-liskov-substitution-principle-in-phplaravel-2o7p</link>
      <guid>https://dev.to/mohasindev/how-to-use-liskov-substitution-principle-in-phplaravel-2o7p</guid>
      <description>&lt;p&gt;In this tutorial, I’ll show you in a very simple way with an example, how you can use LSP(Liskov Substitution Principle) in PHP/Laravel. let’s start walking…&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;At first, let me explain what is Liskov Substitution Principle&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Liskov Substitution Principle is the 3rd principle of the SOLID principle. In SOLID L is stand for the Liskov Substitution principle. Liskov Substitution principle means any implementation of an abstraction or an interface should be substitutable anywhere that the abstraction is accepted. When extending a class, remember that you should be able to pass objects of the subclass in place of objects of the parent class without breaking the client code. This means that the subclass should remain compatible with the behavior of the superclass. When overriding a method, extend the base behavior rather than replacing it with something else entirely. Every class that implements an interface, must be able to substitute any reference throughout the code that implements that same interface. Every part of the code should get the expected result no matter what instance of a class you send to it, given it implements the same interface.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;keynotes of LSP(Liskov Substitution Principle)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Signature must match&lt;/li&gt;
&lt;li&gt;Precondition can’t be greater&lt;/li&gt;
&lt;li&gt;Postconditions at least equal to&lt;/li&gt;
&lt;li&gt;Exception types must match&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Let’s drive into deep&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bSSdQtmj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh9kiep2d97q30i0eiiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bSSdQtmj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh9kiep2d97q30i0eiiw.png" alt="Image description" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You see the picture above is the most familiar &amp;amp; common real-life example for LSP(Liskov Substitution Principle). There is two duck one is real &amp;amp; the other is a rubber duck. Both are looking the same but their activity is not the same. Look at the picture below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DL0VeEYz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqm6aa4f4cn5om0flc7k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DL0VeEYz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqm6aa4f4cn5om0flc7k.png" alt="Image description" width="800" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the picture above you see RubberDuck class extends Duck and has three methods. quack(), fly() and swim().In this class, we are violating LSP as we are overriding the method &amp;amp; throw Exceptions which is not matched with the parent Duck class. Real duck can fly but rubber duck can’t fly and the return type of fly method is not the same so at this point, we are breaking the LSP.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let’s fix the LSP(Liskov Substitution Principle) violation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can extend our code to different interfaces like the picture below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3zaNZ41x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utlb8y72qcgniypeta56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3zaNZ41x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utlb8y72qcgniypeta56.png" alt="Image description" width="800" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the picture above we have three different interfaces are QuackableInterface, FlyableInterface, SwimmableInterface. now we can implement the interface we need for our RubberDuck class like QuackableInterface and SwimmableInterface, as the rubber duck can quack or swim by helping 3rd person but it never can fly so couldn’t implement FlyableInterface. that’s how we can solve the LSP violation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nwNgEKeA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfqkjrsvpgm440bba5t2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nwNgEKeA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfqkjrsvpgm440bba5t2.png" alt="Image description" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, the Liskov Substitution Principle is very simple to follow, it gives the ability for every child class to substitute its parent class. To follow the LSP you just need to be concerned about is, the return type &amp;amp; exception type of any method of a child class must match with its parent class or the interface that is implemented.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mohasinhossain/SOLID-Principles-in-PHP"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>liskovsubstitution</category>
      <category>solidprinciples</category>
      <category>php</category>
      <category>laravel</category>
    </item>
  </channel>
</rss>
