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

Next >>  Class and Object in OOP

 

Tagged with:
 

63 Responses to Object Oriented Programming in PHP

  1. Dan Briz says:

    Nice tutorial Ankur.
    Please write some advantages and disadvantages of oop in php

  2. Nikunj says:

    Very Good!

  3. tom says:

    is it tutorial for php 4 ?? who the hell still uses var to declare properties ??!!

    • Ankur Kumar Singh says:

      Hi Tom,
      Most of the things will not work in php4 like interface, constructor etc. However in few cases we have tried to explain the feature which will not work in php5, but not every where, because php4 is now very old :-)
      However if you still want to try the things in php4 are facing issue, you can ask any question.

  4. ravi says:

    Your tutorials r good. Can you please make tutorials on design patterns. You mentioned that u make them in future. Please make them. cheers.:)

  5. sree says:

    its really good

  6. really… really helpful…. I am trying to learn OOps … today most of the concept related to the class and object are clear now…

    Thanks a lot…

  7. roopa says:

    Please add design patterns in this tutorials

  8. JuN says:

    Great! thanks

  9. The best PHP OOP i had see in 13 years..
    Congrats

  10. farrukh says:

    nice man

  11. sri lakshmi says:

    sri lakshmi
    It is really good it is very helpfull to all of us thank u

  12. kundan says:

    It is really very nice articles…

  13. jaisonsaji says:

    Very useful article. Explained in simple words. Thanks for posting!

  14. ashlin jose says:

    Great…thank you…

  15. Sunil Dabhi says:

    its really good article, nice explanation, thanks.

  16. vetri says:

    Actually i was looking for basic oops concepts in php. now i got it.thanks

  17. kalyandas says:

    very helpful Thanks

  18. Very nice article! In the section where you’re pointing out the benefits of OOP, you do a pretty good job of introducing the concepts of Inheritance and Encapsulation without making the concepts overly jargon-ey. Polymorphism doesn’t seem to show up here, though, and that is definitely one of the major strengths of the Object Oriented paradigm. May I suggest an additional item that details this aspect a bit more?

  19. muslim says:

    good tutoril for me

  20. Rajavenkateshwaran says:

    Great tutorial and well explained with examples. Thanks mate

  21. chandan kumar says:

    all of beginer should read this tutorial. it is the best.

  22. Revan says:

    Very nice tutorial and very useful for beginners.

  23. Thank you for this nice tutorial! You explained it in such a way that even for beginners it’s easy to understand!

  24. Priyesh says:

    Awesome man.. its incredible

  25. Bhawana says:

    You explained it in such a way that even for beginners it’s easy to understand! Thank you for this nice tutorial..

  26. Nitish kaushik says:

    nice and easy to understand

  27. Rishabh Agrawal says:

    This is very awesome tutorial .. thanku very much for post that tutorial.. it is very helpful…and easy to understand.

  28. Malik Talib says:

    zabardast dear. How i can download code where is that link?

  29. S.S.Prabhu says:

    Nice tutorial Easy to Learn

  30. daivuDevendra Verma says:

    very nice

  31. Don McInt says:

    I tried to run this and I get nothing back (I am running PHP 5 and error notices are set to on).
    I checked my code, and even copied and pasted your code and I still get nothing.

    I don’t understand this part of the code:
    return ($this->rate*$this->duration*$->capital)/100; (duration*$->capital)

    shouldn’t it be this:
    return ($this->rate*$this->duration*$this->capital)/100: (duration*$this->capita)

    It’s the “*$->” that I thought was the issue, but changed it to mine and it still didn’t work.

    Here is all of my code, can you tell me my mistake?

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

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

    Thanks,
    Don

  32. Asif Siddique says:

    Dear Don McInt,

    I think you made a mistake in this line…..$calculator=new interestCalculator;
    use semicolon don’t use bracket after your function name(interestCalculator)…..here is the full code…i hope u will get help..

    rate*$this->duration*$this->capital)/100;

    }
    }

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

    ?>

    Thanks
    Asif Siddique

  33. Asif Siddique says:

    rate*$this->duration*$this->capital)/100;

    }
    }

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

    ?>

  34. Asif Siddique says:

    just use a semicolon after $calculator=new interestCalculator();

  35. Hasan says:

    That is the best gift for learner of oophp. Really I am very happy to get it’s destination.

  36. arobind says:

    nice, do you have any project tutorial, pls post it.

  37. Adrian says:

    The OOP had been introduced since PHP 3 not PHP 4, have a look http://php.net/manual/en/history.php.php

  38. Aikey says:

    helpful. thnx

  39. anonymous says:

    its really helpfull,thanx alot buddy…:)

  40. Manoj says:

    thanks, its really helpfull..:)

  41. sharath says:

    really helfull……!…cheers

  42. thank you :) it’s really good

  43. naidu says:

    hey it’s really superb because even in the php main website also not given this type of clarification.
    it’s a good php oops i have seen among all other websites.

  44. manoj says:

    boss mistake in ‘Molecularity: If are are’ u can correct this in the 4th advantage of Advantage of Object Oriented Programming

  45. kalaivanan says:

    very nice tutorial

  46. Scotty says:

    that was very well put together, clear and precise tutorial. thanks a million

  47. divya says:

    i am new to the oops concept , but this tutorial is very much interesting and i can understand , even explained to my friends.. thank you for your contribution..

  48. nawaz says:

    Thats great…. thanks its really helpfull..:)

  49. mukila says:

    thanks for tutorial it is easy to understand. But i want more tutorial about php

  50. i want example programs

Leave a Reply