Summary
If we were asked to name the biggest difference between PHP 4 and PHP 5, we would say without hesitation that it is the introduction of the Zend II language engine. The resulting changes in PHP’s handling of classes and objects are little short of revolutionary. PHP 4 was a procedural scripting language with some capacity to work with basic objects and classes. PHP 5 is a different animal: it has the capability for being used as a fully fledged object-oriented language with polymorphism, inheritance, and encapsulation. The fact that it has managed to achieve these objectives while maintaining almost complete backward compatibility with PHP 4-style classes and objects is amazing.
In this chapter, we covered the most important and useful of these new capabilities, starting with an overview of basic object-oriented concepts. You looked at what classes and objects are, their major parts (members), and how to create them. One major change in PHP 5 from its predecessors is that true encapsulation is now supported; that is, you can control access to class members by declaring them to be public, private, or protected. In addition, you can now force subclasses to implement methods (using abstract classes and methods) as well as prevent subclasses from modifying class members (by declaring methods or classes as final). PHP 5 also allows for a higher level of abstraction by introducing interfaces; just as a class serves as a template for an object, you can think of an interface as a template for a class— or perhaps it is better to think of a class as implementing one or more interfaces.
Another object-oriented feature making its first appearance in PHP 5 is a new way of handling errors. Exceptions, implemented using an Exception class, make it possible to streamline error handling by reducing the number of checks required. They also make it possible to separate error checking from the functional portions of your code. Because the PHP developers wanted to maintain backward compatibility, it is necessary to do a bit of extra preparation to bypass the default error-handling mechanism if you want to use exceptions. However, as you have now seen, doing so is not terribly difficult to accomplish and makes it possible to write much cleaner code than before.
Object-oriented programming is not really complete without a way to obtain information about classes, class instances, and class members, and PHP 4 provided a number of functions to accomplish this. In this chapter, you looked at how PHP 5 retains these functions and adds a few new ones. PHP 5 also introduces a set of classes whose main purpose is to model classes. These classes, known collectively as the Reflection API, make it possible to examine extensions, interfaces, classes, functions, class methods, and properties and their relationships to one another. In addition to introspecting classes, the Reflection API helps facilitate the dynamic generation and manipulation of classes and objects. Both of these capabilities can prove extremely useful when writing generic routines to handle classes and objects whose identity and composition are not known before runtime.
Access provided by Autonomous University of Puebla. Download to read the full chapter text
Chapter PDF
Keywords
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.
Editor information
Rights and permissions
Copyright information
© 2005 Lee Babin, Nathan A. Good, Frank M. Kromann, Jon Stephens
About this chapter
Cite this chapter
(2005). Overview of Classes, Objects, and Interfaces. In: Babin, L., Good, N.A., Kromann, F.M., Stephens, J. (eds) PHP 5 Recipes. A-Press. https://doi.org/10.1007/978-1-4302-0070-3_2
Download citation
DOI: https://doi.org/10.1007/978-1-4302-0070-3_2
Publisher Name: A-Press
Print ISBN: 978-1-59059-509-1
Online ISBN: 978-1-4302-0070-3
eBook Packages: Professional and Applied ComputingProfessional and Applied Computing (R0)Apress Access Books