One of the most common programming concepts in the world is OOP, which stands for Object-Oriented Programming. Using this technique, 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.)
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.
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. But, unlike a class, you won't need to write any code, nor you will see how the class actually works. While you may first think that this isn't so great, in fact this is one of the main concepts of object-oriented programming. You only have to create the class once, then you can create a zillion objects, in a zillion 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;
if($car_object->is_cheap())
{
print "This car is cheap!";
}
else
{
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.
If you like this post then please subscribe to my full feed RSS. You can also subscribe by Email. Not sure how this works?










Hello
After 6 different tutorials of technical material, I finally understand what OO is about. Yours was complete and explained things well.
Thanks,
Thomas Rendleman
Thomas Rendleman
November 14th, 2007
Thomas is right.
Clear, plain, confident an visual.
Thank you
maxskuk
November 16th, 2007
I definately have to hand it to you, your tutorial has been the most clear OOP tutorial i've ever read. If only I had this when I was learning OOP!
Thanks
Luke Taylor
Luke Taylor
November 20th, 2007
Thats a wonderful, straight-to-the-point tutorial on OOP concept of software coding. So simple and inviting.So OOP isnt rockect science anyway.LOL.Thank you man
Adekoye Olaoluwa
November 28th, 2007
Good , it is an excelent tutorial
Yasir Mehmood
December 4th, 2007
Just started PHP a few weeks ago.
Havn't read allot of this tutorial yet, but looking at all the positive replies, going to look through it right away!
Thanks for the share.
Michael
December 22nd, 2007
Now I "really" know what oop really is. Thank you very much…
Guys, hats off to the author!
Dennis Tad
January 2nd, 2008
[…] have a tutorial pertaining to creating a "dummy" class or, if you're lucky, a car class that tells the user whether or not a car is expensive… There are two main issues I have with […]
The next upcoming tutorial: OOP in PHP - Caesar Cipher | WebDev
January 3rd, 2008
Very nice tutorial……
Syed Abdul Baqi
January 16th, 2008
Hi all,
The way of representing the concepts of PHP is very nice. I hope whoever know the concepts of C language can easily understand the concepts of PHP. I hope same type of syntax and examples in before.
With regards,
Saibaba,
Trichy.
saibaba
February 11th, 2008
It is really easy.
Omar faruk
February 15th, 2008
Its interesting and easy to understand
Nitya
March 11th, 2008
Echoing the comments before, a superb tutorial - I've been searching for ages for something about OOP that I understand, and now I've actually found it!
Rob
March 16th, 2008
I agree completely with the above comments. I have studied numerous other sources. This was the best tutorial on OOP. I now understand it. THANK YOU!
John
March 23rd, 2008
Found the tutorial really down to earth ,especially useful for beginners.
kapilgopinath
April 2nd, 2008
nice tutorial bro. . . i learn a lot upon reading this. . . thanks in a million. . .
nash
April 10th, 2008
pretty good, one of the best explanations that I've come across so far. Would also be cool to explain the reason behind the use of __construct, why it's useful to automatically initialize certain properties when an instance of the class is created.
Good job!
rk
April 19th, 2008