Home arrow PHP arrow Page 3 - Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5

Boosting the Existing Capacity - PHP

If you’re a PHP developer who wants to learn how to load automatically into client code all of the source classes required by your object-oriented applications, keep reading. In this series of articles, you’ll learn how to implement the magic “__autoload()” function that comes integrated with PHP 5. With this function, you'll be able to build scripts that can include automatically all of the classes needed by a given application, without using multiple “require()/require_once()” PHP statements.

TABLE OF CONTENTS:
  1. Displaying Meaningful Error Messages when Auto Loading Classes in PHP 5
  2. Establishing a Reference Point
  3. Boosting the Existing Capacity
  4. Developing a Final Example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
December 26, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Boosting up the existing capacity of the “__autoload()” magic function: displaying descriptive error messages

In accordance with the concepts I explained in the previous section, it’s perfectly possible (and desirable, actually) to modify the current signature of the “__autoload()” magic function so that, whenever a given source class can’t be included into client code, it throws the corresponding exception accompanied by a meaningful error message.

Given that, to implement this functionality, I’m going to make a slight change to the signature of the pertinent function, so now it looks like this:


function __autoload($className){

if(!file_exists($className.'.php')){

return eval("class {$className}{public function __construct(){throw new
Exception('Class {$className} not found!');}}");

}

require_once $className.'.php';

}


Not too difficult to grasp, right? As you can see, now all of the exceptions triggered by the above “__autoload()” function will indicate which source class wasn’t loaded at runtime, since the name of the troubling class is simply passed to the constructor of the built-in Exception class.

To illustrate even more clearly how the “__autoload()” function works after introducing the previous modification, below I included an example that displays a “MySQL class not found” error message, when this class can’t be automatically loaded.

The corresponding code sample is as follows:


try{

function __autoload($className){

if(!file_exists($className.'.php')){

return eval("class {$className}{public function __construct(){throw new
Exception('Class {$className} not found!');}}");

}

require_once $className.'.php';

}

// connect to MySQL

$db=new MySQL(array
('host'=>'host','user'=>'user','password'=>'password',
'database'=>'database'));

// fetch users from database table

$result=$db->query('SELECT * FROM users ORDER BY id');

// display user data

while($row=$result->fetch()){

echo 'Id: '.$row['id'].' First Name: '.$row['firstname'].' Last Name: '.$row
['lastname'].' Email: '.$row['email'].'<br />';

}

}

catch(Exception $e) {

echo $e->getMessage();

}


As I said before, if you test the above example on your own computer system and purposely remove the file that contains the pertinent “MySQL” class, then the “__autoload()” function will trigger an exception with the following error message: “Class MySQL not found!”

Indeed, this message is much more meaningful than a raw “Class not found” text, because it gives you a clear idea of which source class couldn’t be correctly loaded by the “__autoload()” function during the execution of a given application.

Okay, at this stage you hopefully learned how to tweak the signature of this handy function to display more descriptive error messages when something goes wrong. However, there are many other approaches that can be implemented to get the same results that you saw earlier.

Nevertheless, in this article I’m going to show you only one more of these additional approaches, which as you’ll see in a moment, will be based on working with the so-called exceptions sub classing.

Do you want to see how this technique will be developed? Click on the link below and keep reading.



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