PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to PHP Subscribe to Newsletters

The Practicality of OO PHP

by David Day
07/28/2005

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:

<?php
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):

<?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() */

require_once("exampleclass.php");

//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:

$exampleobject->set_numbers(1,3);
echo($exampleobject->add_numbers());

//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.

PHP Pocket Reference

Related Reading

PHP Pocket Reference
By Rasmus Lerdorf

Pages: 1, 2, 3

Next Pagearrow




Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

programming

Articles that share the tag programming:

Rolling with Ruby on Rails (1374 tags)

Very Dynamic Web Interfaces (279 tags)

Ajax on Rails (231 tags)

Understanding MVC in PHP (202 tags)

A Simpler Ajax Path (186 tags)

View All

oop

Articles that share the tag oop:

Understanding MVC in PHP (123 tags)

The Practicality of OO PHP (10 tags)

Object Overloading in PHP 5 (5 tags)

Form Your Own Design Pattern Study Group (5 tags)

Static Constructors Demystified (3 tags)

View All

oo

Articles that share the tag oo:

The Practicality of OO PHP (7 tags)

Better Code Through Destruction (6 tags)

Hacking Open Office (2 tags)

Object Overloading in PHP 5 (2 tags)

Introducing Comega (2 tags)

View All

article

Articles that share the tag article:

What Is Web 2.0 (1543 tags)

Rolling with Ruby on Rails (118 tags)

Understanding MVC in PHP (54 tags)

Programming is Hard, Let's Go Scripting... (50 tags)

Very Dynamic Web Interfaces (49 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com