Feb 6

So was recently talking to a friend of mine and he is getting into php, I recommended getting familiar with Object Oriented PHP. I started thinking about all the reasons I prefer it over the procedural style of programming. I realized that for a new user, it seems like more code and more work to even bother with adding objects. Most of the time they want to get what they are doing done and never look back. I know, I used to program exactly like that. Now I have spent a lot of time fixing all the code I made badly five or six years ago. It gets annoying but doing that little extra in the first place saves a lot of fixes later.

On to my intro and reasoning for OOPhp

So a common situation I have run into with procedural programming that causes problems is the overuse of simple functions. One day you make this great little function called display(). The next week you make a new display() function that has slightly different code and you want to use it as well in the same script. So you rename the old display display_old(). This fixes the problem for now, and you only have to go through and fix it here or there. Life is good and it works, that’s what matters.

Except it doesn’t. The next time you make another display() and run into the same issue, all three are used to produce nearly the same result, but each page they are used on needs slight differences. Now you realize that you have written the same code three times and used it three different ways in three different pages. This problem multiplies when you start trying to do updates or other changes to your scripts. We are left with the choice of either keeping each page completely separate, or doing three functions in one require with names like display(), display_page2(), display_old(). It gets ugly quick.

This is one of the cool things about OOPhp, you can use the same object in many different ways while still calling the same function.

Here is an example of an Object that has a display method (function). We can contain both variables and methods in one object. So our code that is slightly different can be contained inside the object rather than having to have multiple functions.

classes/page.php
<?php
class Page {
    public $css;
    public $body;
    public $title;
    public function display(){
        echo '<html>';
        echo '<head>';
        echo '</title>'.$this->title.'</title>';
        echo $this->css;
        echo '</head><body>';
        echo $this->body;
        echo '</body></html>';
    }
}
?>

Now that we have our object, let’s use it to create three different pages.

index1.php
<?php
include 'classes/page.php';
$page = new Page();
$page->css = 'some css file';
$page->title = 'Page 1';
$page->body = 'This is page 1';
$page->display();
?>
index2.php
<?php
include 'classes/page.php';
$page = new Page();
$page->css = 'some different css file';
$page->title = 'Page2';
$page->body = 'This is page 2, it uses the same class as page 1 so it can use the same function, but this will be the output now';
$page->display();
?>

Now that we have an example of what we can do with our object, let’s try out a little more advanced use. Let’s say that the object itself can determine the content to display based on input from the browser. So rather than having a couple static pages, we have one page that takes care of the formatting and such, then what is different between pages is contained in the object.

Again we start with the class. Remember that the class is what we call the code used to create the object.

classes/page.php
<?php
class Page{
    public $css;
    public $title;
    public $body;
    public __construct(){
        $page_name = filter_input(INPUT_GET,'q',FILTER_SANITIZE_STRING);
        if(file_exists('pages/'.$page_name.'.php'))
            require   'pages/'.$page_name.'.php';
        else
            $this->body = 'unable to find page';
    }
    public function display(){
        require 'templates/index.php';
    }
}
?>
pages/page1.php
<?php
$this->css = '';
$this->title = 'Page 1';
$this->content = 'This is the content of page one';
?>
pages/page2.php
<?php
$this->css = '';
$this->title = 'Page 2';
$this->content = 'This is the content of page two';
?>
templates/index.php
<html>
<head>
<title><?php echo $this->title; ?></title>
<?php echo $this->css; ?>
</head>
<body>
<a href="index.php?q=page1">Page 1</a> | <a href="index.php?q=page2">Page 2</a>
<?php echo $this->body; ?>
</body>
</html>

index.php
<?php
require 'classes/page.php';
$page = new Page();
$page->display();
?>

So now we have done a lot, we filtered the input from the browser with the filter_input() command, created different pages using the same object, and are now using a template to format the look and feel of each page in one place. We have also used PHPs object constructor method called __construct(). This method is automatically called when we used the $page = new Page().  The OOPhp style of wrapping up our code into a reusable package is one of its main advantages. It allows you to treat different situations accordingly without having to write out the code all over again. It also helps in the debugging department later on when you have only one class to pound away at to get right rather than 50 functions.

I have put together my two examples below in a zip file if you want to try them out.

OOPhp
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
Comments are off for this post

Comments are closed.