PHP
  Home arrow PHP arrow Page 4 - 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 - Introducing the spl autoload register function
    ( Page 4 of 4 )

     

    As I said in the segment that you just read, the Standard PHP Library comes bundled with a handy function called “spl_autoload_register(),” which allows you to use a callback method or function for automatically loading classes.

    To illustrate how to use this function in more detail, below I defined another sample class called “Autoloader(),” whose “autoload()” method will be tied to the function in question.

    That being explained, here’s how to use this class in a concrete case:

    // example on using the spl_autoload_register() function

    // register autoload() method of Autoloader class

     

    spl_autoload_register('Autoloader::autoload');

     

    // define Autoloader class

    class Autoloader

    {

    public function __construct(){}

     

    // autoload specified file

    public static function autoload($file)

    {

    // the path should be defined as a constant or similar

     $path = $_SERVER['DOCUMENT_ROOT'] . '/loader_classes/';

    $filepath = $_SERVER['DOCUMENT_ROOT'] . '/loader_classes/' . $file . '.php';

    if (file_exists($filepath))

    {

    require_once($filepath);

    }

    else

    {

    Autoloader::recursive_autoload($file, $path);

    }

    }

     

    // try to load recursively the specified file

    public static function recursive_autoload($file, $path)

    {

    if (FALSE !== ($handle = opendir($path)))

    {

    // search recursively the specified file

    while (FAlSE !== ($dir = readdir($handle)))

    {

    if (strpos($dir, '.') === FALSE)

    {

    $path .= '/' . $dir;

    $filepath = $path . '/' . $file . '.php';

    if (file_exists($filepath))

    {

    require_once($filepath);

    break;

    }

    Autoloader::recursive_autoload($file, $path);

    }

    }

    closedir($handle);

    }

    }

    }

    // create instance of User class (it's autoloaded by the Autoloader class)

    $user = new User();

    // display user data

    echo $user;

    There you have it. As you can see, the “autoload()” method defined within the above class has been linked to the “spl_autoload_register()” function, meaning that this method will be called by the PHP parser when attempting to include a specific class.

    Also, the method is capable of performing recursive searches to find the class that needs to be included, as you can see clearly in the prior example. Not too hard to grasp, right?

    Finally, with this wrapping code sample I’m finishing this seventh part of the series. As always, feel free to edit and improve all of the examples shown in this article. This way you can arm yourself with a better background in building class loading programs in PHP 5. The experience will be extremely educational, believe me.

    Final thoughts

    In this seventh installment of the series, you learned how to use the “spl_autoload(),” “spl_register()” and “spl_register()” functions to build an small file loader class which was capable of performing recursive searches through the file system to find a targeted resource.

    In this particular case, the recursive algorithm used by the class was rather lengthy and hard to grasp. However, the Standard PHP library includes a set of iterators that allow you to traverse directories in a truly effortless fashion. So, in the last chapter of this series I’m going to incorporate an iterator into the previous loader class, making its source code a bit more compact and readable.

    Don’t miss the final tutorial!



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