Classes and OOP in PHP

OOP in PHP Object orientated programming (OOP) is a commonly used term when discussing modern programming techniques.

One of the things that makes humans stand out is the ability to categorize – we put objects into categories of similar type of function. For example, we have the category vechicle. Within this category, you have the different types of vehicles – car, truck, train, van. In the same vein, a llama would be categorized as a type of animal. This analogy also applies to programming – we create functions to do specific tasks and then group similar functions into classes. Not only does this organize everything, but also allows a common access point for these similar functions.

OOP is a relatively new type of programming as far as PHP is concerned. PHP4 does not provide the full facilities that a fully object orientated language such as a C++ provides as standard; but PHP5 improves upon this with much more OOP support. Olate Download 2.2 was programmed in a function based style with no classes at all. However, Olate Download 3.0 rewrites the entire application using classes. This allows updates to be made to the class functions without affecting the main code – you don’t necessarily need to know how a class/function does it’s job, just how to use that function.

Classes can’t do anything on their own. They must contain class functions, sometimes called methods. You define these functions just as if you were creating any normal function, however, you need to do it within the class structure.

Lets first create a class called cow. This class will then contain the functions of the cow. The first thing you do is to define the class:

class cow

{

}

As with functions, you encase the functions within the curly braces { and }. There is no semicolon ; anywhere as yet. Also notice that you do not have any parameters, it is class cow not class cow($variable). You do not pass anything to the class itself, you pass it to the functions within the class.

Next, we’ll create our first function – this is defined exactly the same as if you were not using classes:

// First we define the class

class cow

{

// Define the function moo() – no parameters

function moo()

{

// Define the variable

$sound = ‘Moooooo’;

return $sound;

}

}

Now, if we want to use the moo() function of the cow class, we first need to create a new object. When you use functions from a class, you must create an object; hence the name object orientated programming. This is very simple. You create the new class object ‘in’ a variable. So here we go:

// Create new cow object

$daisy = new cow;

This simply creates the object from the cow class within the variable $daisy. So if you want to access any of the cow class functions, you reference them through $daisy. Once we have done this, we can then call the moo() function as so:

echo $daisy->moo();

This will result in Moooooo being echoed to the page. You will notice the -> which effectively says echo the result of the moo() function in the object called $daisy. This is exactly the same as if you had just defined the function normally outside a class and done:

echo moo();

If you put this code together then, you will get something like this:

<?php

// First we define the class

class cow

{

// Define the function moo() – no parameters

function moo()

{

// Define the variable

$sound = ‘Moooooo’;

return $sound;

}

}

// Create new cow object

$daisy = new cow;

echo $daisy->moo();

?>

So what is the point in that? You could have just created a function moo() and not have to mess around with classes. Yes, true, but what happens when you have more functions? A cow doesn’t just moo:

// First we define the class

class cow

{

var $eaten;

// Define the function moo() – no parameters

function moo()

{

// Define the variable

$sound = ‘Moooooo<br />’;

return $sound;

}

function eat_grass($colour)

{

if ($colour == ‘green’)

{

// cow is happy

$this->eaten = true;

return $this->moo();

}

}

function make_milk()

{

if ($this->eaten)

{

return ‘Milk produced<br />’;

}

else

{

return ‘cow has not eaten yet<br />’;

}

}

}

This class is slightly more complicated. It now contains three functions as well as a class variable. After we have declared the class, you will see this:

var $eaten;

You will remember from functions that if you assign a value to a variable within a class, that value is lost once you return from the class. However, we have declared the variable $eaten as a class variable. This is done using the var at the beginning and allows any of the class functions to access and assign values to it. You can see this in action in the eat_grass() function:

$this->eaten = true;

This is setting the $eaten function as true. The fact that it is a class variable is shown by the $this-> at the beginning. As with when we were echoing the result of moo() earlier, this basically means assign true to the class variable eaten.

In the same function, we are using $this->moo(). This is accessing the class function moo() from within another function and returning the result.

Finally, we will call these functions and try out the new class we just wrote:

// Create the cow object daisy

$daisy = new cow;

echo $daisy->moo();

echo $daisy->make_milk(); // Cow has not eaten yet

$daisy->eat_grass(‘green’);

echo $daisy->make_milk(); // Milk produced

You obviously need to include the class definition in the same file as you call the functions so the final version of the file will look something like:

<?php

// First we define the class

class cow

{

var $eaten;

// Define the function moo() – no parameters

function moo()

{

// Define the variable

$sound = ‘Moooooo<br />’;

return $sound;

}

function eat_grass($colour)

{

if ($colour == ‘green’)

{

// cow is happy

$this->eaten = true;

return $this->moo();

}

}

function make_milk()

{

if ($this->eaten)

{

return ‘Milk produced<br />’;

}

else

{

return ‘cow has not eaten yet<br />’;

}

}

}

// Create the cow object daisy

$daisy = new cow;

echo $daisy->moo();

echo $daisy->make_milk(); // Cow has not eaten yet

$daisy->eat_grass(‘green’);

echo $daisy->make_milk(); // Milk produced

?>

And that concludes the PHP classes tutorial! Although I haven’t touched on the advanced features of classes such as constructors, destructors and extending, these are more important when you are creating complex classes and for now, you’ll do fine without them.

Tutorial provided by olate.com/olate.co.uk.

Tags:
Previous Post
SharePoint Platform Services
Business Software

Data Management with SharePoint 2010

Next Post
32 Bit vs. 64 Bit
Hardware

32-bit vs. 64-bit Operating Systems – What is the Difference?

Leave a Reply

Your email address will not be published. Required fields are marked *