Article

The PHP Anthology Volume 1, Chapter 2 - Object Oriented PHP

Page: 1 2 3 4 5 6 7 8 9 10 11 Next

What are the basics of object oriented PHP?

Assuming you have no knowledge of OOP, the best place to start is with the basic PHP syntax for classes. You can think of a class simply as a collection of functions and variables.

Read The Fine Manual

The PHP manual contains a wealth of information on OOP: http://www.php.net/oop

Here, we’ll develop a simple example that could help us generate HTML, which will demonstrate the basics of classes and objects in PHP. This isn’t intended to be an example of great design; it’s simply a primer in PHP syntax. Let’s begin with a procedural script that builds a Web page. Then we’ll gradually turn it into a PHP class:

Example 2.1. 1.php

<?php  
// Generates the top of the page  
function addHeader($page, $title)  
{  
 $page .= <<<EOD  
<html>  
<head>  
<title>$title</title>  
</head>  
<body>  
<h1 align="center">$title</h1>  
EOD;  
 return $page;  
}  
 
// Generates the bottom of the page  
function addFooter($page, $year, $copyright)  
{  
 $page .= <<<EOD  
<div align="center">&copy; $year $copyright</div>  
</body>  
</html>  
EOD;  
 return $page;  
}  
 
// Initialize the page variable  
$page = '';  
 
// Add the header to the page  
$page = addHeader($page, 'A Prodecural Script');  
 
// Add something to the body of the page  
$page .= <<<EOD  
<p align="center">This page was generated with a procedural  
script</p>  
EOD;  
 
// Add the footer to the page  
$page = addFooter($page, date('Y'), 'Procedural Designs Inc.');  
 
// Display the page  
echo $page;  
?>

Of note in this example is our first look at heredoc syntax, which is an alternative method of writing PHP strings. Instead of surrounding the text with quotes, you begin it with <<<EOD and a new line, and end it with a new line and then EOD. The PHP Manual can offer more detail on this if you're curious.

This procedural example uses two functions, addHeader and addFooter, along with a single global variable, $page. Perhaps this isn’t a far cry from procedural scripts you’ve written yourself; maybe you’ve included in every page a file that contains functions such as addHeader and addFooter.

But how do we refactor the above code to take on an object oriented form? (Refactoring is the process of restructuring code without actually changing what it does. This is usually done to ease future maintenance and expansion of the code that would be hindered by its current structure.)

First, we need a class into which we can place the two functions, addHeader and addFooter:

Example 2.2. 2.php (excerpt)

<?php  
// Page class  
class Page {  
 // Generates the top of the page  
 function addHeader($page, $title)  
 {  
   $page .= <<<EOD  
<html>  
<head>  
<title>$title</title>  
</head>  
<body>  
<h1 align="center">$title</h1>  
EOD;  
   return $page;  
 }  
 
 // Generates the bottom of the page  
 function addFooter($page, $year, $copyright)  
 {  
   $page .= <<<EOD  
<div align="center">&copy; $year $copyright</div>  
</body>  
</html>  
EOD;  
   return $page;  
 }  
}

Using the PHP keyword class, we can group the two functions, addHeader and addFooter, within the class. Functions placed inside a class are known as member functions, or, more commonly, methods. Unlike normal functions, methods must be called as part of the class:

Example 2.3. 2.php (excerpt)

// Initialize the page variable  
$page = '';  
 
// Add the header to the page  
$page = Page::addHeader($page, 'A Script Using Static Methods');  
 
// Add something to the body of the page  
$page .= <<<EOD  
<p align="center">This page was generated with static class  
methods</p>  
EOD;  
 
// Add the footer to the page  
$page = Page::addFooter($page, date('Y'), 'Static Designs Inc.');  
 
// Display the page  
echo $page;  
?>

Here, we’ve called the class methods addHeader and addFooter using the :: operator. The script is practically the same as before; however, instead of calling our functions directly, we need to call them as shown here:

$page = Page::addHeader($page, 'A Script Using Static Methods');

Although this isn’t a big improvement, it does let us collect our functions together by their job description. This allows us to call different functions by the same name, each nested separately inside a different class.

Static Methods

So far, we’ve only used a class as a container for related functions. In object oriented parlance, functions that are designed to work this way are called static methods.

Actually, compared to most methods, static methods are about as boring as they sound. In the sections below, we’ll see how you can really flex your object oriented muscles with some fully-fledged methods.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links