Object Oriented Thinking

Posted on Sat, 22 Aug 2009 in java, php by Martijn

Introduction

For quite a number of years, I've been programming in an object oriented fashion. Initially in the first versions of Java and now, as it is maturing more and more, I am also programming in an object oriented style in PHP.

From watching novice programmers and non-programmers, I get the distinct impression that people do not entirely understand what object orientated programming is. So, I thought it might be nice if I gave a small explanation about OO.

This is the first of this kind of article for me, so let me know if you have any suggestions for improvement. Be aware that this is a very basic and simplistic explanation and probably over simplifies things.

What are objects?

Well, look around you for a minute. Just about everything you see can be called an object. Things like a car or bicycle are obvious, tangible, objects. These objects have attributes like their color, the number of wheels or the maximum speed at which they can travel. These objects also have functions like being able to turn on their lights, turn the wheel or start the engine. So, you could say that:

Any object has both attributes as well as functions.

When you look at a bicycle for example, you might notice something: the bicycle is made up of different parts like wheels, gears and a headlight. As you may have guessed, each of these items is another object, each with its own attributes and functions. So:

An object can consist of many other objects.

As I said earlier, these are obvious, tangible objects. Consider for a moment, if you will, something like a story or even this very article. Would you consider this an object? Like tangible objects such as the paper an article is printed on, the actual article (not the paper) has attributes. It has a title, content, length and so on. Does it have a function? You could say that actually being able to read the article contents is a function. So:

Intangible things like an article or thought can often also be described as objects.

What do all of these objects have to do with programming? Computer systems don't run themselves, as you are undoubtedly aware. Clever people in the past and present have developed programming languages that allow a programmer to tell the computer what to do. Over the course of the years, people have evolved various flavors of programming languages and eventually developed so-called object oriented programming languages like, for example, Smalltalk, Eiffel, C++, Java or Python.

The development of these programming languages allows computer programmers to tell computer systems what to do by describing objects. We give these objects attributes (variables) to store information in, and functions (methods) to be able to do things. Lastly, we tell all of these objects how to use each other's functions to work together and do what we want the system to do.

What are classes?

All of these objects are very nice of course, but consider the following example.

As a programmer, you create an object which represents a book. The object has attributes (variables) such as a title, author name, etc. It also has some functions (methods) like retrieving the author name, retrieving the title or retrieving the content.

Now, you are asked to create a computer system for a library containing many thousands of books. If you were to create an object for each of these books by hand, you'd have a steady job for eons. While job security is of course very nice, this way of working is neither exciting nor very efficient.

So, we want to leave the creation of objects to the computer system itself. This would probably necessitate a sentient computer system or a more plausible solution such as classes. So:

A class is a template for an object.

As said, a class is a template as it were of the variables (attributes) and methods (functions) an object contains so the computer system can create many objects of that same class type. The computer uses a class (a template for an object) to create a new instance of an object. So:

An object is an instance of a class.

When you define a class, you define the variables (attributes), which may have default values, and you also define the methods (functions) of that class like, for example, a method that stores the contents of a variable on the harddrive.

How does this work in PHP?

The more recent versions of PHP have had increasingly improved support for object oriented programming. Though PHP isn't quite there yet, most things can already be achieved. So if we were to create a small program to say, create a book object and then display its title, what would we do?

Lets begin by creating a normal index.php file like we would if we created any old PHP program. In this file, we'll define our template, the class, and inside that class define some methods to display the value of some of its variables. Define the class by adding the following code to the PHP file:

public class Book {
}

Not much to look at is there? We've just defined an empty and fairly useless class. It has no variables and no methods. Lets add some variables to it:

class Book {
    public title = 'Undefined';
    public description;
    public content;
}

So we now have a class that defines a title, description and stores the book's contents. You will notice that we have given the title a default value of “Undefined”. Later, when we create an object and don't specify a new value for the title, the object will always return “Undefined” if we ask it its title.

Still not very useful though. Let's add two methods to set and retrieve the value of the title.

class Book {
    public title = 'Undefined';
    public description;
    public content;

    public function setTitle($title) {
        $this->title = $title;
    }

    public function getTitle() {
        return $this->title;
    }
}

I'm assuming you're already aware that $title is a variable. You might be wondering what $this is however. Later on, when we create an object instance from this class (template), the PHP system will use the $this variable to always refer to the object instance in which you are working. So:

$this always refers to the current object.

Very nice. We've now defined a class and have given it variables to store information and even functions to do stuff. But how do we use this class?

First, create a new object instance of type Book. We'll then set the title to a nice value, retrieve it from the object instance and display it on screen.

class Book {
    public title = 'Undefined';
    public description;
    public content;

    public function setTitle($title) {
        $this->title = $title;
    }

    public function getTitle() {
        return $this->title;
    }
}

// Creating a new object instance
$my_book = new Book();

// Giving the book instance a title
$my_book->setTitle('Shogun');

// Displaying the book title
echo 'Book title: '.$my_book->getTitle();

That's the basics of object oriented thinking/programming and how to do it in PHP. Hopefully you will find this article of some use and not too simplistic.