PHP
  Home arrow PHP arrow Page 2 - Using the spl_autoload() Functions to Build Loader Apps 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 spl_autoload() Functions to Build Loader Apps in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 0
    2009-07-01


    Table of Contents:
  • Using the spl_autoload() Functions to Build Loader Apps in PHP
  • Review: the autoload magic function
  • Using the spl autoload extensions and spl autoload functions
  • Introducing the spl autoload register 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


    Using the spl_autoload() Functions to Build Loader Apps in PHP - Review: the autoload magic function
    ( Page 2 of 4 )

    Before I start explaining how to use the “spl_autoload()” and “spl_autoload_register()” functions to build a more complex file loading program in PHP 5, first it would be helpful to reintroduce the example developed in the previous article. It was aimed at demonstrating how to create this kind of application by giving a basic implementation to the “__autoload()” function.

    Here is the signature of the sample “User” class that needs to be included in a fictional application:

    class User

    {

    private $name = 'Alejandro';

    private $email = 'alejandro@domain.com';

     

    // constructor

    public function __construct($name = '', $email = '')

    {

    if ($name != '')

    {

    $this->name = $name;

    }

     

    if ($email != '')

    {

    $this->email = $email;

     

    }

    }

     

    // get user name

    public function get_name()

    {

    return $this->name;

    }

     

    // get user's email address

    public function get_email()

    {

    return $this->email;

    }

     

    function __toString()

    {

    return 'Name: ' . $this->name . ' Email: ' . $this->email;

    }

    }

    And here’s the file loading script that uses the “__autoload()” function for including the “User” class that you just saw:

    // 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 depicted above, it’s obviously mandatory to give a concrete implementation to the “__autoload()” PHP function when dynamically loading a required class. Of course, it’s possible to extend the functionality of the example shown before, but for the sake of brevity I’ll keep it that simple for now.

    At this stage, you've hopefully grasped the logic that stands behind creating class loaders with the “__autoload()” function. But as I stated previously, it needs to be implemented from scratch. However, PHP 5 introduced the “spl_autoload()” function, which offers a default implementation for “__autoload().”

    Does all this sound a bit confusing to you? Don’t worry; in the following section I’m going to set up another code sample, which will clearly demonstrate the usage of the “spl_autoload()” function.

    To see how this brand new example will be developed, click on the link below and keep reading.



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