Popular Blog Posts on The Project Shrink

25 Sure-fire Ways To Motivate Your Team Members

23 Powerful Tips for Working in Multi-Cultural Teams

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.

  • tirupati
    Hello

    After 6 different tutorials of technical material, I finally understand what OO is about. Yours was complete and explained things well.

    Thanks,
    Shekhar Srivastava
  • loganbsc
    It is really nice...Now only i understnand about oops.. thanks friend
  • Prachi Nanhorya
    ya they define it very sensibly and provide us good n brief concept of OOP
  • senthilkumar.g
    hai i like now features and about oops new coding samples
  • sedrick
    I found nice intro to OOP in PHP also here
    http://www.a-scripts.com/index...
  • sad
    sdsad
  • kanishka
    Great
    After searching web in long time finally find the one who explains the concepts very clear and also in understandable way
  • arch
    excellent. cleared my doubts.
  • I, too, agree with all the previous posts.

    Fantastic explanation. Simple, concise, and to the point. Thank you very much!
    *two thumbs up*
  • good example,
    finally, i know what is oop :)
  • Finally, a tutorial which makes sense!, thanks for posting this.
  • Ajit
    It gives confident towards object concept.
  • Ajit
    It is really developing the thinking power and make intrest to know more.
  • Richa Sharma
    i was looking for OO explanation from a long time, and this was the easiest explanations i have been through.keep u the good work.
  • rk
    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!
  • nash
    nice tutorial bro. . . i learn a lot upon reading this. . . thanks in a million. . .
  • kapilgopinath
    Found the tutorial really down to earth ,especially useful for beginners.
  • John
    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!
  • Rob
    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!
  • Its interesting and easy to understand
  • It is really easy.
  • saibaba
    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.
  • Syed Abdul Baqi
    Very nice tutorial......
  • Dennis Tad
    Now I "really" know what oop really is. Thank you very much...

    Guys, hats off to the author!
  • 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.
  • Yasir Mehmood
    Good , it is an excelent tutorial
  • 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
  • Luke Taylor
    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
  • maxskuk
    Thomas is right.
    Clear, plain, confident an visual.
    Thank you
  • 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
blog comments powered by Disqus