Should I Use Object Oriented Programming in PHP

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

When should you use Object Oriented Programming in 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 Object Oriented Programming in 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 Object Oriented Programming in 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 Object Oriented Programming in PHP, however.

 
Benefits of Object Oriented Programming in PHP

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

 
More Productivity

The primary benefit of using Object Oriented Programming in 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.

 
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 Object Oriented Programming in 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):

1
2
3
4
5
6
7
8
9
10
11
12
<?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):

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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. As you can see, Object Oriented Programming in PHP provides a quick and easy outlet for reusing code.



Pages : 1 2 3 4