HomePHP Page 4 - Abstract Classes in PHP: Introducing the Key Concepts
Optimizing the abstract class: using the PHP “get_class()” function - PHP
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.
In order to code a better version of the sample abstract class you saw previously, I’ll simply replace the __CLASS__ magic constant with the PHP “get_class()” function. Here’s the improved example of my “baseClass()”:
class baseClass { function baseClass(){ if(get_class($this)=='baseClass'){ trigger_error('This class is abstract. It cannot be instantiated!',E_USER_ERROR); exit(); } // check if child class attempts to instantiate base class if(!is_subclass_of($this,'baseClass')){ trigger_error('This class is abstract. It cannot be instantiated!',E_USER_ERROR); exit(); } } }
And, if I incidentally tried to instantiate the above class, I’d get the same fatal error:
$baseclass=&new baseClass();
Fatal error: This class is abstract. It cannot be instantiated! in path/to/file
As shown above, the sample class remains nearly the same. Actually, the only difference worth noting is the use of the “get_class()” function instead of the __CLASS__ magic constant, which makes the class slightly more efficient. The final improvement that I could introduce to the class code would be merging the two checking “if” blocks into only one statement, so the overall code is a little tighter. Considering the implementation of this minor change, the final version of the sample abstract class would be rewritten as follows:
class baseClass { function baseClass(){ if(get_class($this)=='baseClass'||!is_subclass_of ($this,'baseClass')){ trigger_error('This class is abstract. It cannot be instantiated!',E_USER_ERROR); } } }
$baseclass=&new baseClass();
Fatal error: This class is abstract. It cannot be instantiated! in path/to/file
As you can see, the above sample class now uses only one checking line to prevent its instantiation, which makes class code even more compact. Additionally, I’ve completely removed the ending “exit()” statement, since the previous “trigger_error()” function is raising a fatal error already, resulting in the script being immediately halted.
At this stage, hopefully I’ve demonstrated how to create a sample PHP 4 class that refuses any attempts to be instantiated, something that reminds us of the definition of an abstract class, which I explained right at the beginning of this article. You'll probably want to study the core concepts and start coding your own abstract classes. Trust me, it’s an enjoyable and educational experience.
Bottom line
And that’s about all for the moment. In this first tutorial, you expanded your knowledge of object-based programming in PHP by actually using all the underlying theory of abstract classes to create a real, useful one. If you’re struggling to understand how abstract classes can be used in PHP applications, I hope this article offered some helpful pointers.
In the second part of this series, I’ll be using abstract classes to set up an instructive example, which will illustrate how to create a hierarchy of classes, handy for processing different types of data. See you in the next part!