Home arrow PHP arrow Page 3 - Throwing Basic Exceptions When Auto Loading Classes in PHP 5

Triggering fatal errors with the _autoload() PHP 5 function - PHP

Welcome to the second part of the series that began with “Auto loading classes in PHP 5.” Comprised of four approachable tutorials, this series walks you through the key concepts you need to understand how to automatically include classes with PHP 5, without having to use the popular include functions that are provided by this powerful server-side scripting language.

TABLE OF CONTENTS:
  1. Throwing Basic Exceptions When Auto Loading Classes in PHP 5
  2. Two examples of the _autoload() PHP 5 function
  3. Triggering fatal errors with the _autoload() PHP 5 function
  4. Developing a simple mechanism to throw exceptions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
December 11, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I explained in the introduction of this article, if an exception is thrown during the execution of the magic “__autoload()” function, it simply won’t be caught by a “try-catch()” block, as you’d normally expect with other PHP native functions. Most likely, the function in question will trigger a fatal error instead, if for whatever reason, a source class can't be included into client code.

But this is only a theory, so see for yourself what happens when the file that contains the previous “Result” class is not available to be included into the sample script that you learned before:

(in this example the “result.php” file is not available)


function __autoload($className){

require_once $className.'.php';

}

try{

// 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();

exit();

}


// Fatal error: __autoload() [function.require]: Failed opening required 'Result.php' (include_path='.;include/path/) in path/to/file


As you can see, the above hands-on example demonstrates in a nutshell what happens when one of the source classes (in this case, the one called “Result”) required by the previous script isn’t available to be included into client code. In simple words, the script will complain about this problem by triggering a fatal error similar to the one show before.

Undoubtedly, from a programmer’s point of view a crude fatal error might not be the best option when it comes to handing a failure that happened when trying to include a specific source class. So, is there a more efficient and elegant solution to deal with this type of error when using the “__autoload()” function? Fortunately, the answer to this question is an emphatic yes!

As you’ll see in the section to come, it’s possible to implement a simple workaround which will make this function throw an exception when, for whatever reason, it fails to load a determined source class.

To learn how this workaround will be developed, please go ahead and read the last section of this article. We’re almost done!



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