writer, web developer, designer

From the Blog

Sep
08

PHP trainer: Object Orientated PHP

Posted by Shell on September 8th, 2011 at 2:56 pm

I thought it was time I took stock of some of the technologies I’ve been working on over the years. Thinking back over the tech I became briefly unstuck with, such as Object Orientated PHP, I thought I’d share some of the basics here. So, what follows are really, really simple code-snippits that may help someone, somewhere, if they’re looking at making the move from function-based PHP to grand world of Object Orientation.

PHP and OOP

Object Orientated Programming has been around forever (I can remember hacking away at OO within Flash years ago). However, PHP has only had OO support in a usable form since PHP 5. If you’re using an older version of PHP, upgrade! It’s not worth the hairloss.

Why Object Orientated PHP anyway?

Anyone who has spent any quality time with PHP knows how beautifully messy the code can get in a remarkably short time. Especially if there are multiple folks working on the code. OOP at the simplest level is possibly the single best way to organise your code by breaking it up into small, distinct libraries of functionality (classes). Smaller units of code can be worked simultaneously, and in theory are easier to debug.

The basics

  • A class is a ‘template’ for building an object.
  • An object is an entity that ‘does stuff’.
  • An object can contain functions (called Methods) and/or data (called Properties).

Classes are usually kept externally from your page code. This enables your classes to be included in any file you wish to access the functionality those classes offer using the PHP include directive.

<?php
  include("classes_lib.php");
?>
									

To create an object in PHP you use the new keyword. Example…

$n = new makeName;

…where makeName is the name of a class. $n is the name of our resulting object. All of the methods and properties that are part of the class makeName are available to $n.

Example

In our example here, I’ll create a simple object that takes a (hard coded) string and spits it back out onto the resulting page. Kind of pointless, but it demonstrates how all of this works in PHP.

index file (index.php):

<?php
  include("classes_lib.php");
?>
<html>
  <head>
    <title>OOP Example</title>
  </head>
  <body>
    <h1><?php displayTitle::title();?></h1>
    <?php
      $n = new makeName;
      $n -> set_name("fred");
      print($n -> get_name());
    ?>
  </body>
</html>
									

class file (classes_lib.php):

<?php
  class makeName {
    var $name;
    // setter
    function set_name($new_name) {
      $this->name = $new_name;
    }
    // getter
    function get_name() {
      return $this->name;
// gets the variable $name declared above. Note the "->" that gets property
//  value from $this object. Use $object -> property / method
    }
  }
  
  class displayTitle {
    static function title() {
      echo "<h1>This is the title</h1>n";
    }
  }
									

A setter is a method used for creating something, perhaps a bit of data, within the object. In our case, we give our setter a string (“Fred”) by calling the set_name method within our newly created $n object:

$n -> set_name("fred");

This tells PHP we want to access the  set_name method and pass a string to it. Once the object contains that string, we can do other stuff with it. In our really, really simple example, we simply get that name back again using:

print($n -> get_name());

The get_name() method is out getter.

I’ve included a second class to generate a title – displayTitle. This class contains a static method – a method that can be called without having to first create an object ($n in our first example). To access a static method, we need to tell PHP the name of the class and the name of the method within that class:

displayTitle::title();

Next step

Hopefully you’ll see that it’s not all that hard to build a reusable object. There are many things you can do with objects, but I’ll save that for another post.

Next steps, we could try and change the class to wrap the name in some pretty HTML. By doing this we then have a nice class that we can use over and over for generating nicely formatted names on the page.

Leave a Reply

  1.  

    |