Monday, 5 August 2013

Object Oriented Programming in PHP

Object oriented programming (OOP) was first introduced in php4. Area for oop in php version 4 was not very vast. There were only few features  available in php4. Major concept of the object oriented programming in PHP is introduced from version 5(we commonly known as php5). Also php community has plan to modify its object model structure in more better manner in php6(not released yet). But still in php5 object model is designed nicely. If you have good understanding of OOP then you can create very good architecture of your php application.  You only need to know some of the basic principles of object oriented programming and how to implement that concept of oop in php. In whole series I will use abbreviation OOP for Object Oriented Programming.
This tutorial series is for both beginners and middle level programmer who want to learn advance concept of oop in php from basic . In this series we will explore all aspect of OOP in PHP from beginning. In every chapter we will covers some best practices. If you are completely beginner for oop please go through every topic of this tutorial series very carefully. If you have some knowledge about the concept of object oriented programming then you can directly go to section of your choice. In every section of tutorial you can download code used in example.

List of Topic/Chapter For OOP In PHP

TopicsDescription
Basics of OOP in PHPIn this part of tutorial you will learn about the very basic concept of OOP in php. This part will cover topic like what is object and class. How to implement class and object in php. Also here we will show you the very basic example for class. If you are beginner for OOP in PHP please you may enjoy this part. This is essential for the beginner for OOP.
Class and Object in OOPIn this part of tutorial you will lean about the basic and advance concept of class and object. It will cover topic like what is class, what is object, how to use implement class and object. This chapter will be started from very basic concept of class and object and will describe upto depth level. This part will cover the best practice of class and object implementation.
Magic Function in PHP  OOPAlthough this is slightly different topic of tutorial but it is included here because some of the magic method is used in object oriented programming. This chapter will cover basic magic methods/function of php which is used in OOP.
Visibility In PHP(Public,  Private and Protected)In this part of tutorial you can learn about the access modifier of OOP in PHP. This part will have complete coverage of what is public, private and protected. Also this will describe how to use these access modifier and best practices.
Static Method and Property in OOPIn this part you will learn about static method and property of the class. In this section you will learn what are the static method and properties, how to use them.
Inheritance in PHPIn this part of tutorial, you will learn basics of inharitance in oop.
Abstract class and Interface PHPIn this part you will learn about interface and class class in php. Here you will learn about what is abstract classes and how and when you should use abstract class and basic concept of interface, How behavior of one class passes to another class
Overloading and OverridingThis part will cover the implementation of overloading and overriding of methods in php. Here you will learn about how to overload method of class and how to override method of class.
Object Cloning PHPThis part will cover how to clone object of your class. In this chapter you will learn about the implementation of object cloning in PHP.
Method Chaining PHPIn this part of tutorial we will explore about method chaining feature of object.

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.
Object in programming is similar to real word object. Every programming object has some properties and behaviors. For example if you have 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 ?

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. Class represents all properties and behaviors of object. For example your car class will define that car should have color, number of door and your car which is an object will have color green and 2 doors. Your car is object of 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 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 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.
  1. 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.
  2. 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 analyse 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.
  3. 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 understanding that if they will set rate and capital property and apply interest calculation method then it will return interest.
  4. Molecularity: If are are creating separate class for your every problem then you are making it modular.So if someone need to change in the business logic part then he will always go to your business logic code part.
Now if you are clear with concept of class, object in oop and its advantages. Great!!!  Its time to move over the implementation of oop in php.

Implementation of OOP in PHP

In this section we will discuss about some basic aspect of oop in php. For every basic aspect we have separate chapter in this tutorial. If you will say basic aspect of oop in php then it is all about classes, objects. For the further detail of oop topic like interface, object cloning etc then you can go to specific chapter of this tutorial. Refer table of contents for the complete list of chapter and its navigation. So let us discuss about basic concept of class, object inheritance here.
Class in PHP:
Class is a blueprint of any object in oop. So class is the first alphabet of oop. In php you can creation of class is very simple. You can create class using tag class. In class block you can define your properties as class variable and function as class behavior. So let us create a class for interest calculator and define its properties like rate, capital, duration and behavior like calculate interest.

class interestCalculator
{
var $rate;
var $duration;
var $capital;
function calculateInterest()
{
return ($this->rate*$this->duration*$->capital)/100;
}
}


Above is a very simple and basic class to calculate interest. Let us explore all basic aspect of this class.
You can create class in php by using class keyword. here class interestCalculator{ } is class block. You can define all of your properties and methods(behavior of class, we will use method or function instead of behavior) of your class inside of your class block. All variable started with var keyword is property of your class. Commonalty we can say these are variable of class also. And the function are methods of this class. You can design your own class with your won variable and function.
Object in PHP:
As we have already discussed that object is an instance of any class. So we will take our interestCalculator class as an example. Creating object of the class is very easy in php. You can create object of class with the help of new keyword. Following is very basic example of creation of object of your class interest calculator:
$calculator = new interestCalculator()
In above declaration you are creating object of your class interestCalculator in variable $calculator. Now your variable $calculator is an object of class interestCalculator. Next step is to set property or variable of object calculator and perform calculation of interest.

$calculator = new InterestCalculator()
$calculator->rate = 3;
$calculator->duration =2;
$calculator->capital = 300;
echo $calculator->calculateInterest();


Here object of your class interestCalculator is your php variable $calculator. In next 3 lines of above code you are setting properties of class. You can access property of class with ->. So in above code rate property is set using $calculator->rate = 3;. Finally after setting all reaqired properties you have called method calculateInterest.
Hope you have clear understanding of oop in php. Download the basic code and run at your machine.
For Indepth Coverage on OOP theory your can further read on wikipedia:
http://en.wikipedia.org/wiki/Object-oriented_programming
original link :- http://www.techflirt.com/tutorials/oop-in-php/index.html#comments

No comments:

Post a Comment