HomePHP Abstract Classes in PHP: Introducing the Key Concepts
Abstract Classes in PHP: Introducing the Key Concepts
An abstract class is a class that cannot (or should not) be instantiated. They are surprisingly useful for certain purposes. In this article, you will learn the important concepts related to abstract classes in PHP 4, and see many hands-on examples that will allow you to make use of them in your own applications.
Although PHP wasnít originally conceived to include many object-oriented features found in full-fledged OOP languages, it has gone through different major rewrites. It now provides developers with a mature, rich language, which offers numerous improvements, particularly in an area known as Object Model. Admittedly, with the long-awaited release of PHP 5, the implementation of a powerful object model has dramatically changed the scenario most PHP programmers must tackle on a daily basis.
The introduction of strong object-based features, such as member visibility, interfaces, and exceptions Ė- to name a few -Ė has brought boosted capacities to all those developers wanting to take a deeper approach to using object-oriented programming. Generally speaking, one of the most interesting topics surrounding the object-based paradigm is the implementation of abstract classes inside of PHP applications, certainly a very useful concept when applied in the appropriate development context.
Throughout this article, the first of a three-part series, Iíll attempt to introduce the core concepts related to abstract classes in PHP 4, explaining some of their key features and exploring their implementation. Due to the fact Iím a loyal believer in the usefulness of code samples, Iíll hook up the corresponding theory to some illustrative hands-on examples, which hopefully will help you to better understand how you can use abstract classes in your own applications.
Of course, since PHP 5 is currently being widely adopted by programmers worldwide, Iíll also cover the use of abstract classes in PHP 5, which might be quite helpful for many developers starting to write code in the latest version of PHP.
By the end of this series, you should have a better idea of how abstract classes fit into the development of certain applications, and hopefully youíll be equipped with the knowledge to decide how and when to put them to work for you.
Interested in learning more about abstract classes in PHP? Letís get started.
What are abstract classes? Introducing the basics
Anyone who has spent considerable time using the object model provided by PHP 4 knows what a class is, but just in case youíve forgotten this concept, letís go over the basics quickly. In PHP a class is merely a set of statements that perform a specific task, and its definition contains both properties and methods, which act as the blueprint from which to create Ė- or instantiate -Ė independent objects.
Thatís the theory for defining a regular class in PHP, so letís go one step further and ask ourselves: whatís an abstract class? In the simplest sense, an abstract class is a class that cannot (and should not) be instantiated. This may sound a little illogical for beginning programmers, but there are situations where this concept makes a lot of sense.
To cite a case where an abstract class can be useful, allow me illustrate a typical situation: youíre developing an application where different classes are organized in a well-structured hierarchy. On top of this hierarchical relationship, you can define an abstract base class, which exposes a given number of generic properties and methods, in order to model the characteristics and behaviors of all its child classes.
From a purist point of view, this abstract class shouldnít provide any explicit definition for its methods or properties. These methods and properties should be specifically defined within the subclasses, which automatically would inherit them from the base class. Indeed, this is a powerful concept that can be very useful when applied in the appropriate development context.
As I said before and strictly speaking, an abstract class should have no specific definitions for its methods. However, there are situations where it is convenient to define a base abstract class on top of the hierarchy, by encapsulating certain functionality when applicable. You would then derive as many classes as needed, which eventually will override or overload the methods inherited from the parent class.
As you can see, abstract classes are very handy for large Web applications, where a well-structured hierarchy of classes is often required. Certainly, this doesnít mean that you have to define abstract classes here and there, whenever you can possibly use them. Most of the time, implementing abstract classes requires careful planning and study of the relationships between the classes that make up an application.
At this point, hopefully youíve grasped the key concepts surrounding abstract classes in PHP. However, as you know, PHP 4 doesnít have the ability to directly declare a class abstract, which implies having to write additional code to prevent such a class from being instantiated. Considering this issue, itís time to go past the underlying theory, and illustrate different approaches to use in PHP 4 to avoid having a class be instantiated. Want to see how this is achieved? Keep on reading.