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
Pages: 1, 2, 3

Should I Use OO PHP?

There are several factors that should determine whether you use OO PHP. For instance, some projects may require the use of OO PHP to ensure a successful completion, while in others, using OO PHP is not required and would only burden the workload.



When should you use OO PHP? When you:

  • have complex functions or code in your project
  • have a large number of functions or code blocks in your project
  • need to keep code organized
  • need to expand the code or project in the future
  • want to reuse your code for other projects
  • find you can't run certain functions more than once on a page without logical errors
  • are a true PHP geek :-)

When should you not use OO PHP? When you:

  • require only a few lines of code
  • will not be expanding your project
  • will not be using the code again
  • are on a very short time frame and your project isn't complex
  • have a boss who tells you not to

These are just suggestions. Use your own judgment when determining whether to use OO PHP. There are a million ways to skin a cat, and the same principle is true with programming: use whatever method works best for you (and those who have to read your code later). There truly are some major benefits to working with OO PHP, however.

Benefits of OO PHP

Among the numerous benefits of using OO PHP, the three that stand out are productivity, organization, and efficiency. With productivity, OO PHP can save time or increase workflow. For organization, OO PHP can keep your code and files organized. By efficiency, OO PHP can make your project and future projects run smoothly.

Productivity

The primary benefit of using OO PHP is the increase in productivity. By this I mean you can save time or boost your workflow. After you create a class file, the next time you use it in a project you've automatically increased productivity, because you've saved the time of having to go back and reprogram something. Now you may think that a list of functions is good enough to save you time--again, I was victim to this early on--but eventually you will find that you could have better used the time spent customizing the function list specifically for the new project. In most cases, a simple class list, carefully programmed, can save you multiple hours of programming repetitive (not to mention really boring) code.

Here are a few examples of productivity that use a small database extraction tool that I whipped up a while back. It originally extracted a single row into a format that a Flash file could read, but I later expanded it for HTML output (having tired of typing repetitive SQL statements). For the sake of having a more interesting example progression, assume that the class starts off with the HTML output ability only. You can download this example class and support files at the end of this article.

Reusability

Reusability in this case is the process of using the same class file for multiple projects. Have you ever needed to reuse your code for another project? Most developers have, and, believe it or not, sometimes it's hard to port your code from one project to another. Functions could be spread out over multiple pages, or complex code could be embedded within HTML, or other various nuisances could make it hard for a smooth reuse of your code. There is a solution with OO PHP.

The process of instantiating classes provides an easy way to implement existing code into a new or already existing project. You don't have to skim through line upon line of existing code to pull out the little snippets needed for your project to function properly. All of your code is within a single file that you can easily include on any page and put to use.

For example, suppose you are using my Data Extraction class for web site A (sitea.php):

<?php
require_once("dataextractionclass.php");

$data = new DataExtraction();

$data->deWhereStatement("WHERE id = '2'")
$data->deExtractDataHtml("users_table", "id", "username", "email");

echo("Hello, ".$test->deGetData("username")."!");
?>

Assuming the user "TestUser" has an ID of 2 in the users_table table, this will output the text Hello, TestUser! If a web site calls for a similar approach in terms of requesting quick information from a web site, you can simply reuse it.

The next example is from web site B (siteb.php):

<?php
require_once("dataextractionclass.php");

$data = new DataExtraction();

$data->deWhereStatement("WHERE email = 'test@example.com'")
$data->deExtractDataHtml("clientlist_table", "id", "email", "balance");

echo("The balance for ".$test->deGetData("email"));
echo(" is $".$test->deGetData("balance")."!");
?>

Assuming the email address test@example.com is in the clientlist_table table with a balance of $2,041, this will output The balance for test@example.com is $2041!

As you can see, OO PHP provides a quick and easy outlet for reusing code.

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