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.

Related Posts
Why some clients thrived during lock down and some did not.
8th August 2020   |   

As a part of a business that interacts with other businesses across industries, on a daily basis, we have a bird’s eye view on how our clients are handling the lockdown. The good news is most of them handled it quite well. It is heartening to s...

Continue Reading
A career as a web design professional in India. List of web design courses and institutes in India.
2nd January 2020   |   

As a web design professional and owner of a web designing firm, I have to be consistent with the ever-changing Web Design Industry. Out of curiosity, I researched about the available Web Designing and Development courses in India. Here is the list...

Continue Reading
The Personal Data Protection Bill 2019 – Implications for online business
18th December 2019   |   

The Personal Data Protection Bill, 2019 was introduced in Lok Sabha by the Minister of Electronics and Information Technology, Mr. Ravi Shankar Prasad, on December 11, 2019. The Bill seeks to provide for protection of personal data of individuals, ...

Continue Reading
Technology is easy. Changing mindsets is difficult.
17th November 2019   |   

We are often caught in the middle of a silent battle happening within our client’s businesses. The battle is between an older generation clinging to a fixed way of doing things and a new generation eager to implement the latest technology, impati...

Continue Reading
Book Reco – What It Takes - Lessons in the Pursuit of Excellence by Stephen A. Schwarzman
24th April 2019   |   

Most autobiographies tend to be written to portray the subject in the best light, glossing over the bad parts and highlighting the good ones and this one is no different. However what makes this such an interesting read is simply the author’s ext...

Continue Reading
The best at website design in India
Connect with us on      
© 2021 Miracleworx Web Design Mumbai. All Rights Reserved.
Quick Enquiry