SEO Packages

Classes and OOP in PHP

October 29, 2011EditorPHP, Web DevelopmentComments Off

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 categorise – 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 vehicle – car, bug, train, van. In the same bag, a llama is a type of animal. This analogy applies to programming – we create functions to do specific tasks and then group similar functions into classes. No only does this organise 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 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:

// 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();
?>

Related Posts

Comments are closed.