This site might not be up to date. New documentation sites are being created: for developers and for users.

Getting Started with Object Oriented Programming

From Joomla! Documentation

PHP is an object oriented language, and to use the MVC structure as implemented by Joomla!, a solid understanding of object oriented programming (OOP) is required. This document explains the reasons for using objects and the way objects are used in PHP and Joomla.

Introduction to Objects

As we venture into object oriented programming, it is important to note that it is called 'object' oriented for a reason.

Before object oriented programming (OOP), everything was based on functions and variables. Let's consider an application that calculates a person's body mass index (BMI). You might have three variables: $height and $weight and $name. These variables would be used to store a person's name, height and weight. You might then have a function called calculateBMI(), which would accept as parameters $height and $weight.

This would look something like:

public function calculateBMI( $height, $weight ) {
    return $weight / $height;
}

But it is easy to lose track of all these variables in the code, not to mention the fact that only one person is supported at a time. The idea behind objects is to encapsulate this data and the functions to manipulate it into one package. The definition of this package is called a class.

So we might have:

class person
{
    public $name;
    public $height;
    public $weight;

    public function getBMI() {
        return $this->weight / $this->height;
    }
}

Then, if you want to create an object which represents a person, you would do:

$person = new person();

This is called instantiating the class, because it creates an instance of the class (an object described by the class).

Now, you can modify the variables (which are called properties), using:

$person->height = 2;
$person->weight = 50;

Then you can invoke its functions (which are called methods) using:

$bmi = $person->getBMI();

Often, many classes are similar, but too different to put into one class. For example, all animals have lungs, and because humans and cats are animals, they could both have a $lung_capacity. However, cats have tails, so should an Animal class have a $tail_length? No, that is not necessary. You can make a class a subclass or child class of another, essentially stating that the subclass has everything its parent class has, plus some extensions.

In Joomla, most classes are children of JObject. Now, if we were to make person a child class of JObject, then we would inherit the capabilities of the JObject class. We would then change the definition to something like:

class person extends JObject
{
    public $name;
    public $height;
    public $weight;

    public function getBMI() {
        return $this->weight / $this->height;
    }
}

Then we could manipulate our person using the get() and set() methods that JObject has:

$person->set( 'name', 'Bob' );
$person->set( 'height', 2 );
$person->set( 'weight', 50 );
$person->get( 'weight' );
echo $person->getBMI();

You will notice the use of $this inside classes a lot. $this is a reference to the current object. So if I am inside a class, and I use say $this->height = 2;, then that means I am setting the property 'height' of the current object to 2. When we use $this->height, we aren't talking about any height, but we're talking about the current object height.

More on Objects

As I said, Objects are called Objects for a reason. If you have a real life object, say a photocopier, there is an external interface (say, a paper tray, the copier glass, the keypad, etc.). Objects in OOP are designed to approximate that setup.

So, I might have a class called copier. Now, what operations do I generally need to do with a copier? Well, the basic functionality I need is copy functionality. So I need a method called 'copy':

class Copier
{
    public function copy() {
        echo 'One copy made';
    }
}

Now, this is a basic copier. What if I wanted to extend the functionality of my Copier? Well, to extend the functionality, I create a child class. A child class will inherit all the functionality of the parent class.

Let's suppose we wanted to create a copier that would keep track of the number of copies it had made. So, we would need to add a property which would keep track of this number, and then we need to somehow adjust this number each time we make a copy.

So here is our child class:

class CopierWithCounter extends Copier
{
    public $counter;

    public function copy() {
        $this->counter++;
        parent::copy();
    }
}

So we now have a property called $counter that keeps track of the number of copies made. Notice that the method we defined has the same name as the method in the Copier class. What this means is that we don't need to learn anything new to use the copier - it behaves in the same way as our old copier, but it just keep track of the number of copies.

You will see that inside the copy() method there is a line: 'parent::copy()'. The parent keyword references the parent class, which is in this case Copier. So this line will invoke the copy() method of the Copier class. In this way, we don't have to rewrite the functionality to make a copy - we have already done that in the Copier class.

So we have the exact same functionality as the Copier class, except that anytime a copy is made it will increment the $copies property by 1. Just as in real life, the addition of the counter doesn't change the way that I use the copier - I don't need to know anything about the counter to just make a simple copy.

Now, the question arises: what value does $counter have to start with? We know that it increases by one every time a copy is made, but that is all we know.

This value needs to be initialized to a certain value.

Initializing values is generally done by what is called a constructor. A constructor does just that - it constructs the object. In PHP5, constructors are functions with the name __construct. We will use __construct here.

So our class now becomes:

class CopierWithCounter extends Copier
{
    public $counter;

    public function __construct() {
        $this->counter = 0;
    }

    public function copy() {
        $this->counter++;
        parent::copy();
    }
}

Now suppose we wanted to create an even more advanced copier. We can do that by creating another child class. Let's create a copier that is able to do multiple copies. In order to do this, we need a way to specify how many copies we want and a way to remember this number. We will add a method called 'setCopies()':

class CopierMultipleCopies extends CopierWithCounter
{
    public $copies;

    public function setCopies( $copies ) {
        $this->copies = $copies;
    }
}

We now have a way to specify how many copies we want to make.

Now, it is appropriate here to say a word about scope. When we talk about scope, we talk about where a certain variable can be seen. You will notice that in our class, we have a property called $copies. But we also have a parameter called $copies in our method setCopies. How do you tell them apart?

Well, the rules of scope tell us which variable we are talking about. If a method takes a parameter, say $copies (as above), then if I use $copies inside that method, I am referring to that parameter. There may be other variables called $copies that are defined in other places, but I don't care about those - I only care about the one inside my function. If I want to refer to a property of the current object, I use the $this keyword. So, if I use $this->copies, then I am talking about the $copies property that belongs to my current object.

So that aside, our setCopies() method will allow us to set the number of copies that we want to make using our copier. The method takes one parameter - $copies, and stores it in the object.

You will notice that our current class definition for CopierMultipleCopies doesn't define a copy() method or a constructor. But, because it extends CopierWithCounter, it inherits the copy() method from CopierWithCounter, and also inherits the properties. So, without doing any extra work, we already have a Copier with a counter.

But we still want to extend the functionality of the copy() method so that it actually makes the multiple copies. So, we add a method definition to our class. We will also add a constructor that will add the functionality of initializing the number of copies to 1.

class CopierMultipleCopies extends CopierWithCounter
{
    public $copies;

    public function __construct() {
        $this->copies = 1