PHP
  Home arrow PHP arrow Page 3 - 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 - Using the spl autoload extensions and spl autoload functions
    ( Page 3 of 4 )

    As you saw in the preceding section, it’s pretty simple to implement the “__autoload()” function and build a class loading program very quickly. However, the Standard PHP library includes the “spl_autoload()” function, which has a default implementation for “__autoload().”

    To help you understand how this function works, below I coded a whole new example. It shows how to dynamically include the “User” class that you saw before. The example in question looks like this:

    // example on using the spl_autoload() function

    // register file extensions

    spl_autoload_extensions('.php,.inc');

     

    // default implementation for the __autoload() function

    // load User class

    spl_autoload('user');

    // create instance of User class

    $user = new User();

    // display user data

    echo $user;

    If you closely examine the above script, you’ll realize how simple it is to work with the “spl_autoload()” function. First, the script uses the “spl_autoload_extensions()” to set up the appropriate order of the file extensions for which the PHP engine will look when attempting to load an given class. This can be very convenient for speeding up the whole loading process.

    Finally, the “spl_autoload()” function is utilized to dynamically include the aforementioned “User” class via a default implementation of the previous “__autoload().”

    Obviously, when it comes to creating class loaders in a few simple steps, the “spl_autoload()” function can be a real time-saver. But what if you need to define a custom method for including your classes? Well, that’s a really easy problem to solve, thanks to the existence of another function of SPL, called “spl_autoload_register().”  As its name suggests, it permits you to register a custom callback function for autoloading classes.

    In the last section of this tutorial I’m going to cover the use of this function in detail. Therefore, to learn more about it, click on the link below and keep reading.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





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