PHP DevCenter
oreilly.comSafari Books Online.Conferences.


The Practicality of OO PHP

by David Day

There seems to be a common pitfall among some PHP developers--especially those just beginning PHP programming--and that is their lack of object-oriented (OO) PHP use. This article's purpose is to inform developers about the practicality of OO PHP; fully understanding the benefits of using OO PHP should be a requirement in the PHP learning process.

Pitfall Among Developers

I myself experienced the pitfall in the beginning of my PHP enlightenment, but the light shone again and my eyes opened to the use of OO PHP. What I mean by OO PHP specifically is the use of classes, not just simple function blocks. Many new developers feel that classes are useless, cumbersome, and time-consuming. Given the unique nature of how PHP actually works, it is possible to throw functions in the code at any point on the page and start using them immediately. If this is the case, it is possible to include a page of functions at the top of the page and go to town with them. Now, while it is possible to do this--and using classes isn't very far from it in some aspects--there are numerous benefits and advantages to actually using classes instead of simply using a list of functions.

What PHP Objects Are All About

To new developers, classes--or object-oriented programming, for that matter--may seem a little intimidating or unfamiliar. For those of you who aren't familiar with classes, here's a brief introduction.

Please note that PHP 5 has a new Object Model from PHP 4. This section refers to PHP 4, so if you have upgraded to the new version of PHP, please refer to the official PHP documentation.

Here's a file called exampleclass.php. Class definition looks like this:

class Example
    //class-wide variables
    var $var1;
    var $var2;
    //function to gather two numbers
    function set_numbers($number1, $number2)
        $this->var1 = $number1;
        $this->var2 = $number2;
    //function to add numbers together
    function add_numbers()
        return ($this->var1 + $this->var2);

Under the class-wide variables comment are two variable initializations: var $var1; and var $var2;. Variables defined in this manner are available throughout the entire class. You can access them in any function by using the syntax $this->variablename.

For example, the function set_numbers() assigns the $number1 argument to the classwide variable var1. The function add_numbers() returns the sum of var1 and var2.

To use this class, create an instance of the Example class object (exampleuse.php):

/* Include the class file using require_once. 
It will require the class to be loaded (or
the page won't load), and will only allow 
it to be included once, to avoid errors. You
can also use require(), include(), or 
include_once() */


//create an object variable for the instance
//of the example object

$exampleobject = new Example;

This creates an object variable using the new function. The syntax is simple: $objectvariable = new ClassName;. As you can see, the name of the class in exampleclass.php is Example; this is the name you must use to assign the class to the object variable. With an instance of the Example class, you can use the functions (or methods) inside of it:


//the output will be: 4


Using class methods is very similar to the $this-> operator, except that $this is replaced with the object variable. In this example, the object variable is $exampleobject. Both $exampleobject->set_numbers(1,3) and $exampleobject->add_numbers() call methods in this instance of the Example class.

For more in-depth info on PHP classes, please refer to the official PHP documentation.

Pages: 1, 2, 3

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: