Home arrow PHP arrow Page 2 - Abstract Classes in PHP: Introducing the Key Concepts

Creating abstract classes in PHP 4: using some simple approaches - 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.

TABLE OF CONTENTS:
  1. Abstract Classes in PHP: Introducing the Key Concepts
  2. Creating abstract classes in PHP 4: using some simple approaches
  3. Defining a clever abstract class: preventing instantiation from non-subclasses
  4. Optimizing the abstract class: using the PHP “get_class()” function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 30
January 25, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

A simple method for coding a class that can’t be instantiated consists of including a simple “die()” statement inside its constructor. The example below demonstrates a simple way to implement this method:

class baseClass {
    function baseClass(){
        die('This class is abstract. It cannot be
instantiated!');
    }
}
 
// try to instantiate abstract class
$baseclass=&new baseClass();

The above example shows a very simple approach to preventing a class from being instantiated, by including a “die()” statement within the class constructor. After trying to instantiate an object from the sample “baseClass”, I get the following message:

This class is abstract. It cannot be instantiated!

And logically, the script is simply halted. Moreover, the above method is also effective if I ever want to statically call the constructor, by using the resolution scope operator (::), as listed below:

baseClass::baseClass();

This class is abstract. It cannot be instantiated!

In this case, the “die()” statement again displays a warning message and stops the execution of the script. For the first attempt at creating an abstract class, it’s not bad at all. However, a “die()” statement does not provide much control on program execution, so let’s move on and code another example of an abstract class. This time, we'll use the “trigger_error()” PHP built-in function. Have a look:

class baseClass {
    function baseClass(){
        if(strtolower(__CLASS__)=='baseclass'){
            trigger_error('This class is abstract. It cannot be
instantiated!',E_USER_ERROR);
            exit();
        }
    }
}
// try to instantiate abstract class
$baseclass=&new baseClass();

Fatal error: This class is abstract. It cannot be instantiated! in path/to/file

As shown by the above example, I’ve coded the sample class so it triggers a fatal error whenever there’s an attempt to instantiate it. Here, I’ve used the magic constant __CLASS__ in order to prevent the class from being instantiated, which raises a fatal error when this happens.

Similar to the first example you saw earlier, if I ever want to statically call the class constructor, I get the same result:

baseClass::baseClass();

Fatal error: This class is abstract. It cannot be instantiated! in path/to/file

So far, I’ve defined a sample class that triggers a fatal error and, in consequence, halts the script when trying to spawn an object from it. This means that I’m very close to defining an abstract class in PHP 4. However, there are some issues to be addressed yet. The current definition of the sample class doesn’t prevent calling the constructor from a non child class. Taking this limitation into account, the next few lines will show you how to fix this problem. Scroll down the page and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: