HomePHP Abstract Classes in PHP: Setting Up a Concrete Example
Abstract Classes in PHP: Setting Up a Concrete Example
Welcome to part two of the series “Abstract classes in PHP.” In three tutorials, this series introduces the key concepts of abstract classes in PHP 4-PHP 5, and explores their application and use in different object-oriented development environments. Whether you’re an experienced PHP developer wanting to fill in some gaps related to abstract classes, or only a beginner starting to taste the power of object-based programming in PHP, hopefully you’ll find this series enjoyable and instructive.
As you’ll probably recall, in the first article I thoroughly explained the key points for implementing abstract classes in PHP 4, and demonstrated different methods of using them within a PHP application. Since PHP 4 doesn’t offer true support for abstract classes, that is, classes that can’t (and shouldn’t) be instantiated, I went through the development of different approaches for preventing a specific class from direct instantiation. In accordance with this situation, I illustrated a few common approaches to prevent a class from being instantiated, ranging from including a “die()” statement inside the class constructor, to developing more polished techniques, such as utilizing a combination of the “get_class()” and “is_subclass()” PHP built-in functions.
As you know, this last method was also aimed at preventing a class from being instantiated from non child classes. This implies having a decent approach for using abstract classes, particularly in PHP 4 programming environments, where such classes aren’t natively supported.
Now, returning to this second installment of the series, and assuming that abstract classes aren’t foreign concepts to you any longer, I’ll go one step further in the corresponding learning curve. I will set up a couple of more helpful examples, which I hope will serve as an introduction to using PHP 4 abstract classes in some concrete situations, something definitely much closer to the needs that most PHP developers have to face on a daily basis.
Right, having defined the objectives of this tutorial, it’s time to move on and learn how abstract classes can be put to work in some concrete situations. Let’s get started.