DEV Community

Cover image for How To Use Liskov Substitution Principle in PHP/Laravel
Mohasin Hossain
Mohasin Hossain

Posted on

How To Use Liskov Substitution Principle in PHP/Laravel

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…

At first, let me explain what is Liskov Substitution Principle

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.

keynotes of LSP(Liskov Substitution Principle)

  1. Signature must match
  2. Precondition can’t be greater
  3. Postconditions at least equal to
  4. Exception types must match

Let’s drive into deep

Image description

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

Image description

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 & 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.

Let’s fix the LSP(Liskov Substitution Principle) violation.

We can extend our code to different interfaces like the picture below.

Image description

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.

Image description

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 & exception type of any method of a child class must match with its parent class or the interface that is implemented.

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.

Top comments (0)