Skip to main content

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. The major concept of the object oriented programming in PHP is introduced from version 5(we commonly known as php5). Also, php community has the plan to modify its object model structure in a better manner in php6(not released yet). But still, in the php5 object model is designed nicely. If you have the good understanding of OOP then you can create a 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

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.
Type Hinting in PHPIn this part of tutorial we will explore how to implement type hinting in php
PHP NamespaceIn this part of tutorial we will explore how to use namespace in PHP.

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.

  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 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.
  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 understood that if they will set rate and capital property and apply interest calculation method then it will return interest.
  4. 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.

Implementation of OOP in PHP

In this section, we will discuss some basic aspect of oop in PHP. For every basic aspect, we have a separate chapter in this tutorial. If you will say the 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 the specific chapter of this tutorial. Refer table of contents for the complete list of chapter and its navigation. So let us discuss basic the 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 a clear understanding of oop in PHP. Download the basic code and run on your machine.

For Indepth Coverage on OOP theory your can further read on wikipedia:

Next >> strpos in php

Next >> interface in php

Next >>ย curl in php

Next >>ย inheritance in php

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

85 thoughts to “Object Oriented Programming in PHP”

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

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

  2. 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?

  3. 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();


  4. 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)… is the full code…i hope u will get help..



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


    Asif Siddique

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


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


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

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

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

  9. Really nice explanation. But I found one typo mistake!!

    There is one line: return ($this->rate*$this->duration*$->capital)/100;

    which should be : return ($this->rate*$this->duration*$this->capital)/100;

    “this” was missing in $->capital

    Thanks again.

Leave a comment/Ask Question