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

Defining a clever abstract class: preventing instantiation from non-subclasses - 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

Since my original intention was to create a class that cant be directly instantiated, even when called from non subclasses, Ill build a very simple (and yet very useful) sample class, one that uses the PHP is_subclass() function, in order to avoid instantiation from non child classes. This new version of my sample class looks like this:

class baseClass{
    function baseClass(){
        if(strtolower(__CLASS__)=='baseclass'){
            trigger_error('This class is abstract. It cannot be
instantiated!',E_USER_ERROR);
            exit();
        }
        // check if non 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();
        }
    }
}

$baseclass=&new baseClass();

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

As you can see, now the sample class introduces additional checking code, in order to avoid allowing non subclasses to instantiate the base class. To achieve this, Ive utilized the PHP is_subclass() function, which comes in handy for determining whether the base class is being called from a subclass.

At this point, Ive created a base class that cant be directly instantiated, and additionally refuses instantiation from any non child classes. However, Im not completely satisfied with the code utilized by the class. It uses the __CLASS__ magic constant, which sometimes can lead to unexpected results, depending on the function with which this constant is included. Thus, lets move forward and rewrite the code of the above sample class, so its able to use the PHP get_class() function, which, as youll see, will result in a more robust implementation of the abstract class.



 
 
>>> 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: