If you've never used the object-oriented programming model, PHP 5 comes as a major revelation. This three-part article introduces you to the language's OOP features and explains their advantages. It is excerpted from chapter six of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).
This chapter and the next introduce what is surely PHP 5’s shining star: the vast improvements and enhancements to PHP’s object-oriented functionality. If you’ve used PHP prior to version 5, you may be wondering what the buzz is all about. After all, PHP 4 offered object-oriented capabilities, right? Although the answer to this question is technically yes, version 4’s object-oriented functionality was rather hobbled. Although the very basic premises of object-oriented programming (OOP) were offered in version 4, several deficiencies existed, including:
An unorthodox object-referencing methodology
No means for setting the scope (public, private, protected, abstract) of fields and methods
No standard convention for naming constructors
Absence of object destructors
Lack of an object-cloning feature
Lack of support for interfaces
In fact, PHP 4’s adherence to the traditional OOP model is so bad that in Jason’s first book, A Programmer’s Introduction to PHP 4.0, he devoted more time to demonstrating hacks than to actually introducing useful OOP features. Thankfully, version 5 eliminates all of the aforementioned hindrances, offering substantial improvements over the original implementation, as well as a bevy of new OOP features. This chapter and the following aim to introduce these new features and enhanced functionality. Before doing so, however, this chapter briefly discusses the advantages of the OOP development model.
■Note While this and the following chapter serve to provide you with an extensive introduction to PHP’s OOP features, a thorough treatment of their ramifications for the PHP developer is actually worthy of an entire book. Conveniently, Matt Zandstra’s PHP 5 Objects, Patterns, and Practice (Apress, 2004) covers the topic in considerable detail, accompanied by a fascinating introduction to implementing design patterns with PHP and an overview of key development tools such as Phing, PEAR, and phpDocumentor.