Basics of OOP in PHP
Object oriented programming is nothing but a technique to design your application. Application could be any type like it could be web based application, windows based application. OOP is a design concept. In object oriented programming, everything will be around the objects and class. By using OOP in php you can create modular web application. By using OOP in php we can perform any activity in the object model structure. There are many benefit of using oop over the parallel or procedural programming. Further in this part we will cover some basic of object and class and its implementation in php.
What is Object?
If you want to see theoretical definition of object described in the typical book of oop then following is the best definition of object is:
Any thing is the world is an object. Look around and you can find lots of object. Your laptop, pc, car every thing is an object. In this world every object has two thing properties and behaviors. Your car has property (color, brand name) and behavior(it can go forward and backward). If you are able to find properties and behaviors of real object. Then it will be very easy for you to work with Object Oriented Programming.
In real world different objects has different properties and behaviors. For example your television has property size, color, and has behavior turn on, turn off. If you observe carefully then you can find that every object has some property and behavior from other object. This phenomena is called inheritance. For example car object has property and behavior from engine object.
If you are able to understand what is object then good to go ahead. If not then please start visualizing properties and behavior of object around you. You will defiantly understand.
The object in programming is similar to real word object. Every programming object has some properties and behaviors. For example, if you have an object for interest calculator then it has property interest rate and capital and behavior simple interest calculation and compound interest calculation. Interest calculator has some property and method from calculator object like addition, multiplication etc.
What is Class ?
The class is something which defines your object. For example, your class is Car. And your Honda car is object of car class. Like object explanation, here we will take an example of the real word and then we will move further in programming definition.
Blueprint of the object is class. The class represents all properties and behaviors of the object. For example, your car class will define that car should have color, the number of doors and your car which is an object will have color green and 2 doors. Your car is an object of a class car. Or in terms of programming, we can say your car object is an instance of the car class. So structural representation (blueprint) of your object is class.
Now let us take an example of the programming. Your interest calculator object is an instance of class interest calculator. Interest calculator class defines properties like capital, rate, and behavior like simple interest calculation and compound interest calculation. Your interest calculator object has property rate as 3% and capital 300 USD. So you are describing your class definition of the rate by giving rate value equals to 3% and capital 300USD in your interest calculator object. Now in your object when interest calculation behavior will be applied it will take your rate of interest and capital and provide you the result. Again your interest calculator class will inherit the definition of its property and behavior from calculator class.
Advantage of Object Oriented Programming
There are various advantage of using OOP over the procedural or parallel programming. Following are some of the basic advantages of using oop techniques.
Re-Usability of your code: If you will use OOP technique for creating your application then it will gives you a greater re-usability. For example, if you have created calculator class at one place then you can use the same calculator class in your application.
Easy to Maintain : Application develop using oop technique are easier to maintain than normal programming. Again let us take an example of your interest calculator class. Suppose your business need to change the calculation logic. They want to add some charges if your capital is less than 200 USD. Just think about your application is big and developed using normal programming techniques. So first you have to analyze that at how many places we have calculated interest, and then you will change. But just think of oop technique. You just need to change in your method of interest calculation at one place.
Good Level of Abstraction: Abstraction means making something hidden. By using oop technique you are abstracting your business logic from implementation. It will provide you greater ease. Again let us take and example of interest calculator. If you have created class for interest calculation and your team is going to use that class. Now you are only concern about how interest calculation will be performed because you have created that. Your team member is always have understood that if they will set rate and capital property and apply interest calculation method then it will return interest.
Molecularity: If are creating a separate class for your every problem then you are making it modular.So if someone needs to change in the business logic part then he will always go to your business logic code part.
Now if you are clear with the concept of class, object in oop and its advantages. Great!!! Its time to move on the implementation of oop in PHP.