PHP
  Home arrow PHP arrow Page 4 - 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 - Optimizing the abstract class: using the PHP “get_class()” function
    ( Page 4 of 4 )

    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!



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek