The previous three-part article series introduced you to the fundamentals of object-oriented programming with PHP. This three-part series takes a look at the advanced OOP features offered by PHP 5. It is excerpted from chapter 7 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).
Chapter 6 introduced the fundamentals of object-oriented PHP programming. This chapter builds on that foundation by introducing several of the more advanced OOP features that you should consider once you have mastered the basics. Specifically, this chapter introduces the following five features:
Object cloning: One of the major improvements to PHP’s OOP model in version 5 is the treatment of all objects as references rather than values. However, how do you go about creating a copy of an object if all objects are treated as references? By cloning the object, a feature that is new in PHP 5.
Inheritance: As mentioned in Chapter 6, the ability to build class hierarchies through inheritance is a key concept of OOP. This chapter introduces PHP 5’s inheritance features and syntax, and includes several examples that demonstrate this key OOP feature.
Interfaces: An interface is a collection of unimplemented method definitions and constants that serves as a class blueprint of sorts. Interfaces define exactly what can be done with the class, without getting bogged down in implementation-specific details. This chapter introduces PHP 5’s interface support and offers several examples demonstrating this powerful OOP feature.
Abstract classes: An abstract class is essentially a class that cannot be instantiated. Abstract classes are intended to be inherited by a class that can be instantiated, better known as a concrete class. Abstract classes can be fully implemented, partially implemented, or not implemented at all. This chapter presents general concepts surrounding abstract classes, coupled with an introduction to PHP 5’s class abstraction capabilities.
Reflection: As you learned in Chapter 6, hiding the application’s gruesome details behind a friendly interface (encapsulation) is one of the main OOP tenants. However, programmers nonetheless require a convenient means for investigating a class’s behavior. A concept known as reflection provides that capability, as described in this chapter.
Advanced OOP Features Not Supported by PHP
If you have experience in other object-oriented languages, you might be scratching your head over why the previous list of features doesn’t include one or more particular OOP features that you are familiar with from other languages. The reason might well be that PHP doesn’t support those features. To save you from further head scratching, the following list enumerates the advanced OOP features that are not supported by PHP and thus are not covered in this chapter:
Namespaces: Although originally planned as a PHP 5 feature, inclusion of namespace support was soon removed. It isn’t clear whether namespace support will be integrated into a future version.
Method overloading: The ability to implement polymorphism through functional overloading is not supported by PHP and, according to a discussion on the Zend Web site, probably never will be. Learn more about why athttp://www.zend.com/php/ ask_experts.php.
Operator overloading: The ability to assign additional meanings to operators based upon the type of data you’re attempting to modify did not make the cut this time around. According to the aforementioned Zend Web site discussion, it is unlikely that this feature will ever be implemented.
Multiple inheritance: PHP does not support multiple inheritance. Implementation of multiple interfaces is supported, however.
Only time will tell whether any or all of these features will be supported in future versions of PHP.