logo

"We need a web site."

Now what?
When you have plenty of ideas, but not a lot of answers, the next step is WeavingtheWeb.com.

Enter your email address here to subscribe to the "PHP Tip of the Week," emailed to you every Monday morning. Enjoy!

 

PHP Tip of the Week Archive

  • If Brevity: The Ternary Operator
  • Easy Database Inserts
  • Output Buffering and Editing Web Pages On the Fly
  • Searching Arrays
  • Random Thoughts
  • Faster Page Transfer Using Compression
  • Persistent Values in Web Forms
  • Using HEREDOC Syntax with HTML
  • Calculating Dates More Easily
  • Using Single and Double Quotes
  • Timing Your PHP Scripts
  • When Nothing is Something: Zero, Empty, Null, and Negative Values
  • Nesting Instincts
  • Useful (for) Characters: The Increment Operator
  • The "static" keyword
  • Those !#@% Characters!
  • The Value of PHP Classes and OOP

    I once heard a PHP developer mention that he saw no practical use for PHP “classes” (not the sit-down type at your local community college, but rather those used in object-oriented programming or OOP). He reasoned that he could accomplish every programming task he needed with functions and variables alone, apart from classes. And he complained that classes tended to obfuscate the logic of PHP code, making it difficult for others to understand and manipulate programs.

    I sympathize with my friend. PHP classes and object-oriented programming do take a bit more patience to unravel if you’re trying to decipher someone’s class-laden code.

    But apart from demonstrating your PHP prowess by using classes, are there any practical advantages to using them? There are, and I think they are compelling.

    1. So much open source code uses PHP classes.

    I’m constantly on the lookout for good open source packages for specific development tasks: content management systems, blogging software, discussion boards, calendar programs, and on and on. Almost without exception, these programs rely heavily on PHP classes. Why?

    Before answering that question, here’s the obvious point: if you’re using or modifying any open source programs, you’ve got to understand object-oriented programming…unless you relish writing applications from scratch when free, downloadable, feature-laden programs are available at the click of a mouse.

    The sun rises, water runs downhill, and PHP classes are everywhere.

    2. The use of classes forces planned, logical program development.

    And maybe that’s why some of the best open source programs use PHP classes. Their organization and structure allow for flexible, extensible programming that can accommodate multiple contributors and so yield a better, more powerful program.

    I think it was Larry Wall of perl programming fame that says there are always at least ten ways to accomplish the same programming task. That’s true, but not every approach is “best” when you consider its logic, its propensity for future error as more code is added, its clarity if others should later need to modify it, and its runtime performance. Because OOP tends to be more structured, it forces programmers who use it to think and plan ahead accordingly. And that’s always good.

    3. The use of classes encourages extensible, reusable PHP code.

    One of the great strengths of OOP is inheritance, or the ability to “extend” a base class to include the variables and functions of another class. Thus a base class might accomplish a general task (like creating a shopping cart for an online store) and an inherited class might then accomplish more specific tasks (like adding products to the cart created by the base class).

    Examples of class inheritance are beyond the scope of this short article, but look here for illustrations:

    http://www.php.net/manual/en/language.oop.php
    http://www.php.net/manual/en/keyword.extends.php

    Bottom line: when you program with PHP classes, you may find yourself reusing more and more of your code on other projects. And you may begin to consider how you can structure your classes so that you can reuse your code elsewhere. If that saves you time and blood and sweat and tears, well, that’s good, right?

    Like I said, I sympathize with my class-resistant friend. PHP classes require a little more effort up front, but the rewards are well worth the investment.

  • The Simple Things in (programming) Life
  • Just a Passing Reference
  • Simple Image Creation with PHP