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.
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 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
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 = 'firstname.lastname@example.org'") $data->deExtractDataHtml("clientlist_table", "id", "email", "balance"); echo("The balance for ".$test->deGetData("email")); echo(" is $".$test->deGetData("balance")."!"); ?>
Assuming the email address
email@example.com is in the
clientlist_table table with a balance of $2,041, this will output
The balance for firstname.lastname@example.org is $2041!
As you can see, OO PHP provides a quick and easy outlet for reusing code.