Home arrow PHP arrow Page 4 - Using the _autoload() Magic Function to Build Loader Appps in PHP

Implementing an enhanced exception mechanism - PHP

Welcome to the sixth part of an eight-part series that shows you how to build file loader applications in PHP. In this part I will discuss how to build a helpful file loading application by taking advantage of the nifty __autoload() magic function.

TABLE OF CONTENTS:
  1. Using the _autoload() Magic Function to Build Loader Appps in PHP
  2. Review: building a file loader class the old way
  3. Loading files with the autoload PHP magic function
  4. Implementing an enhanced exception mechanism
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 24, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

As I expressed in the section that you just read, a good epilogue for this tutorial will be demonstrating how to implement a better exception handling mechanism within the auto-loading script built earlier. So, bearing this concept in mind, below I developed a whole new code sample. It shows how to throw more insightful exceptions within the __autoload() function when the file to be included isnt found. Heres the example in question:

 

// 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 shown before, the implementation of the __autoload() function now allows you to throw an improved exception if the file that needs to be included by the calling script isnt found. It's short to code and read, indeed. 

And with this example, Im completing this sixth chapter of the series on building file loading applications in PHP. As usual, feel free to tweak all of the code samples shown in this tutorial, so you can practice developing this kind of web-based program.  

Final thoughts  

Thats all for the moment. Over this sixth part of the series I discussed how to build a small - yet helpful -- file loading application in PHP 5 by taking advantage of the nifty __autoload() magic function, which made the whole development process a breeze. 

I have to say, though that Im only scratching the surface when it comes to creating file loaders with PHP 5. The language provides yet another pair of powerful functions that are part of the Standard PHP Library, called spl_autoload_register() and spl_autoload_register(). These functions can be used for building clever file loading applications very quickly.  

In the next article Im going to explore these functions in depth, so now that you know what to expect from the upcoming tutorial, you wont want to miss it! 



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