PHP
  Home arrow PHP arrow Page 4 - Using the _autoload() Magic Function to Build Loader Appps in PHP
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

Using the _autoload() Magic Function to Build Loader Appps in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-06-24


    Table of Contents:
  • Using the _autoload() Magic Function to Build Loader Appps in PHP
  • Review: building a file loader class the old way
  • Loading files with the autoload PHP magic function
  • Implementing an enhanced exception mechanism

  • 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


    Using the _autoload() Magic Function to Build Loader Appps in PHP - Implementing an enhanced exception mechanism
    ( Page 4 of 4 )

     

    As I expressed in the section that you just read, a good epilogue for this tutorial will be demonstrating how to implement a better exception handling mechanism within the auto-loading script built earlier. So, bearing this concept in mind, below I developed a whole new code sample. It shows how to throw more insightful exceptions within the “__autoload()” function when the file to be included isn’t found. Here’s the example in question:

     

    // define a generic exception handler function

    function exception_handler($e)

    {

    echo $e->getMessage();

    }

     

    set_exception_handler('exception_handler');

    function __autoload($class)

    {

    $file = $class . '.php';

    if(!file_exists($file)){

    return eval("class {$class}{public function __construct(){throw new Exception('Class {$class} not found!');}}");

    }

    require_once($file);

    }

     

    // create instance of User class

    $user = new User();

    // display user data

    echo $user;

    As shown before, the implementation of the “__autoload()” function now allows you to throw an improved exception if the file that needs to be included by the calling script isn’t found. It's short to code and read, indeed. 

    And with this example, I’m completing this sixth chapter of the series on building file loading applications in PHP. As usual, feel free to tweak all of the code samples shown in this tutorial, so you can practice developing this kind of web-based program.  

    Final thoughts  

    That’s all for the moment. Over this sixth part of the series I discussed how to build a small –- yet helpful -- file loading application in PHP 5 by taking advantage of the nifty “__autoload()” magic function, which made the whole development process a breeze. 

    I have to say, though that I’m only scratching the surface when it comes to creating file loaders with PHP 5. The language provides yet another pair of powerful functions that are part of the Standard PHP Library, called “spl_autoload_register()” and “spl_autoload_register().” These functions can be used for building clever file loading applications very quickly.  

    In the next article I’m going to explore these functions in depth, so now that you know what to expect from the upcoming tutorial, you won’t want to miss it! 



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

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - 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...





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