PHP
  Home arrow PHP arrow Page 3 - Abstract Classes in PHP: Introducing the Key Concepts
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Abstract Classes in PHP: Introducing the Key Concepts
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 27
    2006-01-25


    Table of Contents:
  • Abstract Classes in PHP: Introducing the Key Concepts
  • Creating abstract classes in PHP 4: using some simple approaches
  • Defining a clever abstract class: preventing instantiation from non-subclasses
  • Optimizing the abstract class: using the PHP “get_class()” function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Abstract Classes in PHP: Introducing the Key Concepts - Defining a clever abstract class: preventing instantiation from non-subclasses
    ( Page 3 of 4 )

    Since my original intention was to create a class that can’t be directly instantiated, even when called from non subclasses, I’ll 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, I’ve 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, I’ve created a base class that can’t be directly instantiated, and additionally refuses instantiation from any non child classes. However, I’m 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, let’s move forward and rewrite the code of the above sample class, so it’s able to use the PHP “get_class()” function, which, as you’ll see, will result in a more robust implementation of the abstract class.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek