Why to learn Object-oriented PHP ?

Why to learn Object-oriented PHP ?

Before moving further we should must know what i am going to learn and why it is useful to learn ?

Object Oriented programming(OOP) gives us code re-usability feature. It means write a code and use it where you need it.

Lets take a scenario to understand how OOPS is helpful for code re-usability.

Suppose we have two vehicle AUDI, and BMW and we have to process them through the form.
In procedural approach, we will first write code for AUDI and then BMW. While, the same can be done in OOPS as

Create a base class named Vehicle and then we write the common properties of both vehicle which we required, like (color, price, weight e.t.c). Now we will consider each property (color,price,weight) as method (function customary) so that we can store these values in database by passing these values in the (color, price, weight) method as parameter.

Now we will create two more class for AUDI, BMW and inherit the methods(color, price, weight) of the base class(i.e : Vehicle) and extend it with the method that are unique to them.

Done, Now lets suppose we have working code of this scenario in OOPS as well Procedural.

Scenario – 1 : Now Client’s custom requirement is to add one more car i.e Mercedes.

Procedural :  We will write the same code for Mercedes as we did for BMW and AUDI (i.e we have                           to rewrite all color, price and weight codes again).
OOP :             We will extend vehicle class with Mercedes class and inherit the methods of vehicle                            class  i.e(color, price, weight).

Scenario – 2 : Now client wants to add the noOfWheel for all the vehicle and save it to database.

Procedural : We will edit all the forms i.e (Audi, BMW, Mercedes) and rewrite code for all vehicle.

OOPS :  We will add a new method noOfWheel in the base class, since we have already extended vehicle class with AUDI, BMW and Mercedes. So they will be automatically updated.

There are so many advantages of OOPS over Procedural approach :

  • Code Re-usability.
  • Isolation of codes.
  • Structured way of coding.
  • Easier to manage when there are frequent changes in project.

But wait….

Procedural have their own advantage at some situations like :

  • For smaller projects procedural approach is preferable.
  • When there would not be any future changes in project.

Leave a Reply

Your email address will not be published. Required fields are marked *