Home arrow PHP arrow Page 2 - Improving Exception Throwing when Auto Loading Classes in PHP 5

One Step Backward - PHP

This is the third article in the series on how to auto load classes in PHP 5. This article will demonstrate how to trigger exceptions in a way that can be caught by the corresponding "catch()" block.  Please keep reading to find out more.

  1. Improving Exception Throwing when Auto Loading Classes in PHP 5
  2. One Step Backward
  3. Improving the Definition
  4. The Improved Source Code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
December 18, 2007

print this article



Going one step backward: throwing exceptions with the “__autoload()” function independently of a “try-catch()” block

Before I proceed to teach you how to throw exceptions from inside the corresponding “__autoload()” function, which can then be intercepted correctly by a “try-catch()” block, I’d like to list the full source code of the example I developed in the preceding tutorial of this series, where this functionality wasn’t implemented yet.

In doing so, it’ll be much easier for you to grasp the small changes that I’m going to introduce to the definition of the “__autoload()” magic function, which will provide it with the capacity to launch the aforementioned exceptions.

All right, having said that, please take a close look at the following code sample, which implements a basic exception mechanism within the pertinent “__autoload()” function:

(definition of mysql.php file)

class MySQL{

private $host;

private $user;

private $password;

private $database;

private $connId;

// constructor

function __construct($options=array()){


throw new Exception('Connection options must be an array');


foreach($options as $option=>$value){


throw new Exception('Connection parameter cannot be empty');






// private 'connectDb()' method

private function connectDb(){


throw new Exception('Error connecting to MySQL');



throw new Exception('Error selecting database');



// public 'query()' method

public function query($sql){


throw new Exception('Error running query '.$sql.' '.mysql_error());


return new Result($this,$result);



(definition of result.php file)

class Result{

private $mysql;

private $result;

// constructor

public function __construct($mysql,$result){




// public 'fetch()' method

public function fetch(){

return mysql_fetch_array($this->result,MYSQL_ASSOC);


// public 'count()' method

public function count(){


throw new Exception('Error counting rows');


return $rows;


// public 'get_insertId()' method

public function getInsertId(){


throw new Exception('Error getting insert ID');


return $insId;


// public 'seek()' method

public function seek($row){


throw new Exception('Invalid row parameter');



throw new Exception('Error seeking row');


return $row;


// public 'getAffectedRows()' method

public function getAffectedRows(){


throw new Exception('Error counting affected rows');


return $rows;



(definition of autoload_example.php file)



function __autoload($className){


eval("class $className {}");

throw new Exception('Class not found');


require_once $className.'.php';


// connect to MySQL

$db=new MySQL(array

// fetch users from database table

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

// display user data


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



catch(Exception $e){

echo $e->getMessage();



As you’ll probably recall, the previous hands-on example implements a basic database-driven application that is given the task of fetching some basic data on a few fictional users from a simple MySQL table, which are then displayed on the browser. In addition, I concretely implemented the “__autoload()” function at the beginning of this example so it can be used to include the respective “MySQL” and “Result” classes required by this application in the client code (keep in mind that these reside on different PHP files).

Now that you've hopefully recalled how the previous database-driven application does its business, suppose that for any reason the file that contains the pertinent “MySQL” class has been removed from the file system.

Based on this hypothetical and certainly conflictive scenario, when the “autoload_example.php” file is parsed by the PHP 5 interpreter, it will complain loudly because this class simply won’t be available and consequently, the following fatal error will be triggered:

Fatal error: Function __autoload(MySQL) threw an exception of type 'Exception' in path/to/file

Much better, but not very elegant, right? In this case, the “__autoload()” function implements a simple workaround that allows it to throw an exception when trying to include the missing “MySQL” class, even when it can’t be properly caught inside the corresponding “try-catch()” block.

Obviously, from a programmer’s point of view, throwing exceptions that can’t be intercepted by one “try-catch()” block is pointless. In this case, it’s necessary to introduce some additional improvements to the definition of the “__autoload()” magic function, so it can be provided with the capacity to launch exceptions that can be trapped within a “try-catch()” structure.

Bearing in mind that this issue needs to be fixed quickly, in the upcoming section I’m going to slightly modify the signature of the “__autoload()” function to implement the aforementioned feature.

To see how these changes will be incorporated into this handy function, please 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


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