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