PHP
  Home arrow PHP arrow Page 4 - The Autoload Magic Function in PHP 5
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

The Autoload Magic Function in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-06-22


    Table of Contents:
  • The Autoload Magic Function in PHP 5
  • Review: the destruct method
  • Loading classes automatically with the autoload magic function
  • Loading classes in the background with the autoload 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


    The Autoload Magic Function in PHP 5 - Loading classes in the background with the autoload function
    ( Page 4 of 4 )

    In the section you just read, I defined a class called “User.” Admittedly, it doesn’t perform any particularly useful tasks, but it's handy for demonstrating how it can be loaded in the background by the “__autoload()” function.

    To accomplish this however, it’s necessary to give this function a concrete implementation, which will be very easy to understand. Take a look at the following example, which shows how to turn the “__autoload()” function into a more useful piece of code:  

    function __autoload($class)

    {

    $file = $class . '.php';

    if(!file_exists($file))

    {

    eval("class $class {}");

    throw new Exception('Class ' . $class . ' not found.');

    }

    require_once($file);

    }

    There you have it. As you can see, all that I did before was define the “__autoload()” function so it can load in the background the file corresponding to a class whose signature hasn’t been found by the PHP interpreter.

    If the referenced class isn’t found, then an exception will be thrown, and eventually it will be caught either by a “catch()” block or by a custom exception handler. Logically, in the last instance a “require_once()” function is called within “__autoload(),” but it must be coded only once. Otherwise, in a typical scenario, one of these should have been coded per each requested file.

    Now that you've surely grasped the underlying logic of the previous “__autoload()” function, and assuming that the following script and the file that contains the “User” class resides in the same location on the file system, loading the class in the background would be as simple as this:

    // create instance of User class

    $user = new User();

    // display user data

    echo $user;

    Short to write and read, isn’t it? In this case the script creates a new instance of the “User” class and displays the values assigned to its properties. This happens with only one single class. However, where the “__autoload()” function really shines is when it comes to including multiple classes within the same script, since it lets you get rid of the annoyance of this process.

    And with this final example, I’m finishing this round up of the most important magic functions that come included with PHP 5. If you haven’t used them yet when developing your own web application, I encourage you to give them a try, particularly for overloading properties and methods in a straightforward –- yet careful -- way. You won’t be disappointed, trust me!

    Final thoughts

    Nothing lasts forever, and this series isn’t an exception. Nonetheless, in the end I hope the experience has been instructive. In this round up you learned how to implement and use the most relevant magic functions provided by PHP 5.

    As with other features of the language, don’t abuse these functions, particularly when overloading methods and properties of a class. This can lead to making your code less readable and harder to maintain for other developers.

    Don’t miss the next PHP development tutorial!



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