PHP Classes: An Introduction to OOP

25. Apr, 2010

OOP she did it again

OOP: she did it again

There is still much debate about Object Orientated Programming (OOP) with PHP, largely because PHP wasn’t developed as an OOP language, and it works perfectly well as a procedural language for most web applications or websites that you are creating. On top of that, on face value, OOP can seem daunting and overly-elaborate, with many more lines of code needed to achieve something simple, compared to the procedural way.

That said, OOP PHP can become really useful on a community level, because generic-purpose classes can be created with all sorts of uses, such as easily creating word documents, creating pdfs, flash interaction, image/audio manipulation, and, well, basically anything you could dream of.

To get a picture of the power of php classes, you could maybe take a moment to look around the aptly named www.phpclasses.org, where anyone can use or contribute their own php classes with each other, creating a repository far more powerful than a single brain could muster.

So, it’s worth understanding the OOP principals and syntax within PHP, whether you want to start adhering to strict OOP protocols in the future, or if you simply want to understand the basics of OOP in order to be able to dabble into other people’s classes when it takes your fancy.

For this tutorial we’re going to introduce PHP classes, creating a simple class that prints out a reusable, customisable textbox for a website.

The PHP Class

The class is the crux of OOP, and in simple terms is a collection of variables and functions that are related to each other. Here is an example of a lovely PHP class:

class infobox {
    $variable = 1;
    $another = 2;
}

And to access the data inside this class, you can use the following syntax:

$infobox = new infobox;
echo $infobox->variable;
echo $infobox->another;

Here we create a new infobox instance, and then we can echo the variables inside it with the rather exciting “right arrow”, which will become your new OOP best friend. Of course, classes can become a lot more complex, and a lot more useful, but this principle, that the class is grouping two variables together in a family, is the basis for all OOP.

Now we are going to make a class that has a little more purpose. We want to make a class that will print out a textbox with a header and a body text, which has default colours that can be changed using functions.

First of all, we’ll set up some CSS, just to make things look pretty. Note that we’re not defining the infobox’s width or background colour, or the h2 element’s text colour. These will be defined by the PHP class.

body {
    background:#CCC;
    color:#333;
    font-family:Arial, Helvetica, sans-serif;
    font-size:20px;
    letter-spacing:-1px;
}
.infobox {
    padding:5px;
    margin-bottom:20px;
    -moz-border-radius: 10px; /* For Firefox */
    -webkit-border-radius: 10px; /* For Safari & Chrome */
    -khtml-border-radius: 10px; /* KHTML */
    border-radius: 10px; /* CSS3 */
}
p {
    padding-top:20px;
}

Now that this is out the way, we can create a class. The variables of this class will define what can be customised by the user, in this instance, the width of the infobox, the background colour, and the text colour of the infobox’s heading.

class infobox {
    var $width = 300;
    var $bgcolour = "c3ffc4";
    var $h2colour = "71a873";
}

Now we need to create a function within this class that will print out an infobox every time it is invoked. Let’s have a look at that:

class infobox {
    var $width = 300;
    var $bgcolour = "c3ffc4";
    var $h2colour = "71a873";
    function output($heading, $body) {
        $p = '<div class="infobox" style="background:#' . $this->bgcolour . '; width:' . $this->width . 'px;">';
        $p .= '<h2 style="color:#' . $this->h2colour . ';">' . $heading . '</h2>';
        $p .= '<p>' . $body . '</p>';
        $p .= '</div>';
        echo $p;
    }
}

We are greeted with a new syntax here, “$this->variable.” When operating functions within a class, you can access all the variables that belong to the class with this syntax. So here we use “$this->colour”, to access the variable $colour from the top of the class.

The function output() simply creates an echoed string that contains a div, an h2 tag and a p tag. The tags contain some user defined variables. It also sets the colours and width of the infobox using the default, set variables of the class. Let’s see how we can use this.

$infobox = new infobox;
$infobox->output('Title for the Box','Some body text for the box');

Here, we create a new instance of the class, $infobox, and we access the function output() in the same manner as we access the variables of the class, with a right arrow. We pass two variables into the function, one for the title, one for the body, and we end up with an output like this:

Now, what if we want to change the default variables within this class? We can create some new functions that will make this happen.

    function changeWidth($newWidth) {
        $this->width = $newWidth;
    }

If we put this into our class, we have a new function to access, that will set the $width variable of the class to our new variable. So we could run:

$infobox = new infobox;
$infobox->changeWidth(500);
$infobox->output('Title for the Box','Some body text for the box');

And now, instead of having the default 300 pixels wide infobox, we have an infobox that is 500 pixels wide. We can create similar functions to alter the background and text colours, and end up with a class like this:

class infobox {

    var $width = 300;
    var $bgcolour = "c3ffc4";
    var $h2colour = "71a873";

    function output($heading, $body) {
        $p = '<div class="infobox" style="background:#' . $this->bgcolour . '; width:' . $this->width . 'px;">';
        $p .= '<h2 style="color:#' . $this->h2colour . ';">' . $heading . '</h2>';
        $p .= '<p>' . $body . '</p>';
        $p .= '</div>';
        echo $p;
    }
       
    function changeWidth($newWidth) {
        $this->width = $newWidth;
    }
   
    function changeBgColour($newColour) {
        $this->bgcolour = $newColour;
    }
   
    function changeh2Colour($newColour) {
    $this->h2colour = $newColour;
    }
   
}

Now we have a class with default settings, and the ability to change all three settings with a function, and then a main function for outputting the data, so whenever an infobox is needed to be echoed into your application you can make it happen from one class. Let’s give it a test run:

$infobox = new infobox;
$infobox->output('Title','This is some body text');
$infobox->changeWidth(500);
$infobox->output('New Title','Some more body text for the second infobox');
$infobox->changeBgColour('a4a4a4');
$infobox->changeh2Colour('000000');
$infobox->output('Third Title','More text for the final infobox, with different colours!');

Here we output three infoboxes. The first has the default settings, the second has the width changed to 500, and the third has the colours changed to greys and blacks.

And there we have it, your first introduction to php OOP, using the php class to create something practical for your site development. Of course this is very simple and doesn’t even start to touch on the complexity of classes and their power, but it does explain the benefits and theory behind the PHP class, and why it can be more useful than simple procedural methods.

Categories: php

2 Responses to “PHP Classes: An Introduction to OOP”

  1. neil pearce 27. Jul, 2010

    Just read one of your tutorials on nettuts and thought it was very good. Better still are these ones you have written – nice one!

    – Neil

  2. admin 27. Jul, 2010

    Cheers Neil! Sooner or later I’ll put some new ones up here too. As soon as I get a moment to spit…


Leave a Reply