Object Oriented Programming In PHP
6th January 2010

Object Oriented Programming, as the name implies, is about programming with objects.

But what exactly is an object?

An object is an entity that encapsulates properties and behaviour that is specific to that entity.

This might sound a little strange, but it really is not. Objects are all around you. The magazine you are holding is an object. Some objects can consist of even more objects. The articles in magazine are objects. The window in your room is an object. Your room is an object. Everything you can see is an object. But there is a lot more to OOP as a concept.

Object = variable + functions

Using this OOP, programmers manipulate objects, which are made of functions and variables, instead of manipulating the functions and variables themselves. Let's say that you develop an e-commerce web-site. You could use an object to manage a shopping cart, and assign the object different properties and methods, based on what you want it to do. Properties stand for variables holding the information about the object (for example: the name, items in the cart, total value of the items, etc.), and methods stand for the functions that can be used with the object (for example: add an item into the cart, remove an item, empty cart, etc.)

Class = blueprint for one or more objects

It sounds simple, doesn't it? Well, it really is. But for an object to be defined, you have to have a template on which you will define the object. This is where classes come in. A class is a blueprint for one or more objects. Therefore, an object is to a class what a variable is to a type. A class is a set of characteristics, and an object is entity that is defined based on those characteristics. Another example: let's think about an automobile class. Such a class could have a characteristic (property) called "color". All objects created based on this class would have such a characteristic, but some objects would initialize this property to "red", others to "blue", and so on. This means that the class only holds a definition, and the object holds the actual value.

You can declare a class by using the "class" keyword. Let's define a simple class:

class automobile_class


var $color; //the color of the car

var $max_speed; //the maximum speed

var $price; //the price of the car, in dollars

function is_cheap()


return ($this->price < 5000); //returns TRUE if the price is smaller than 5000 dollars



In this small example you can notice some of the most important aspects of a class. After the declaration of the class, you can see the variables used within the class, which are called properties. These are declared using the "var" statement. While they can be defined anywhere within the class, you should really define them at the very top, so you can better see the class' properties. The functions within the class are called methods; they're used to manipulate the class' properties and produce results. In that simple method you can see that when we use a class method or property, we must use the "->" operator. The keyword "this" tells PHP that the property of method belongs to the class being defined that means variable belongs to the whole class and not the function itself. $this->contents is a variable which was defined as part of the overall class.

An object is a special variable that contains a bundle of other variables and functions; you always have to use a class upon which to create an object. You only have to create the class once, then you can create a millions of objects, in a millions of other projects.

While a class only exists in code and is considered to be a blueprint, an object exists in memory and is a working instance of a class. An instance of an object is created using the "new" statement along with the name of the class the object is based on. Let's return to our automobile class:

$car_object = new automobile_class();

$car_object->color = "red";

$car_object->price = 6000;



print "This car is cheap!";




print "This car is expensive!";


You can see that we use the "->" operator to access and modify object's properties. After that, we use the same operator to call a method.

Perhaps the greatest benefit of object-oriented code is its reusability. Because the classes used to create objects are self-enclosed, they can be easily pulled from one project and inserted into another. Additionally, it is possible to create child classes that inherit and/or override the characteristics of their parents. This technique allows you to create more complex and specialized objects. Even if you start with a small class, you can develop it to a complex class by time, with adding more properties and objects to its children classes.

Advantages of OO approach:

Reusability: Objects can stand on their own. They are abstracted; they represent one thing. This means that they can be combined in many ways, which makes for (and encourages) reusability. Reusing objects rather than having to reinvent the wheel over and over again can save a lot of time.

Extendibility: Instead of writing a completely new object every time you need one (which often takes quite some time), you can often extend one. It is in the nature of objects that they are extendable. One can derive an object from another object, and thus extend its functionality without having to rewrite the whole object and add the required functionality.

Maintainability: Because of the very natural way, objects (and their hierarchies) can be designed, they are easy to read, which makes it easier to analyse, and thus extend already existing applications. Because of the "pluggable" nature of objects, less code modification is needed to integrate new features into an application.

Final Words:

Object Oriented Programming is a powerful and often elegant approach to analyze, design, and implement a solution to a problem, but not something you master in days, weeks, or even years. It is certainly not the golden key to solve every problem, that would be too good to be true. It is not realistic to apply OOP theory to that tiny script that makes sure the form is being emailed. It might help you when designing average to large scale applications though.

About Me

He has been in the web development business for the almost two decades.He is a keen student of marketing and business development and writes regularly on web strategy and other related topics which is read and followed by many every month.

About Author

The best at website design in Mumbai
MiracleworX Web Design Mumbai
Connect with us on      
© 2024 Miracleworx Web Design Mumbai. All Rights Reserved.
Request A Quote Connect With Us
Quick Enquiry