Home arrow PHP arrow Page 4 - The Autoload Magic Function in PHP 5

Loading classes in the background with the autoload function - PHP

Among the improvements and new features that were introduced to PHP 5, there’s a set of special functions, popularly known as magic functions. These allow you to perform all sorts of smart tasks, ranging from overloading properties and methods in classes, to using destructors and triggering automatically predefined processes when serializing and unserializing objects. This is the conclusion to a seven-part series that shows you how to use the magic functions in PHP 5.

TABLE OF CONTENTS:
  1. The Autoload Magic Function in PHP 5
  2. Review: the destruct method
  3. Loading classes automatically with the autoload magic function
  4. Loading classes in the background with the autoload function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 22, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: