Search This Blog

Monday, September 13, 2010

Object Oriented Programming In PHP

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.

No comments:

Post a Comment