Improving Exception Throwing when Auto Loading Classes in PHP 5

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.

The introduction of PHP 5 has rapidly facilitated the development of object-based applications due to the plethora of improvements incorporated into its robust object model. In this way it provides PHP programmers with the features required to get the most out of the object-oriented paradigm.

Working with objects during the development of a given PHP 5-driven application implies that these must be spawned using their corresponding originating classes. They are frequently loaded into client code by using the "includes" functions provided by this programming language.

However, when working with medium and large scale applications, manually loading a bunch of classes to perform concrete tasks means that each of them may have to be associated with a “require()/require_once()” statement. At times, this can be an annoying and time-consuming process.

In response to the above issue, PHP 5 comes packaged with the handy “__autoload()” magic function. When implemented in a clever way, this can be really useful for automatically including all of the source classes required by a given object-based application without having to call the pertinent “require()” PHP functions multiple times.

If you already read the previous article of this series, this advantage should be quite familiar to you. I explained not only how to utilize the “__autoload()” magic function using a friendly approach, but demonstrated how the implementation of this function can be tweaked to provide it with the capacity to throw exceptions when one source class can’t be properly included into client code for whatever reason.

Unfortunately, throwing exceptions within the “__autoload()” function implies having to implement some quick and dirty solutions. The most popular is based on declaring the class that the PHP 5 interpreter failed to include, in this case using the “eval()” PHP native function, within the function in question.

Obviously, there are other approaches available that obligate the “__autoload()” function to throw an exception, but I decided to cover the one that dynamically declares the failed class within the function in question, since I consider it the simplest to implement and learn. Naturally, if you feel inclined to use a different technique to throw exceptions that can be properly intercepted within a “try-catch” block in conjunction with the mentioned function, it’s only a matter of deciding what fits your particular needs.

All right, at this point you’ve learned how to launch exceptions when the “__autoload()” magic function is called by the PHP 5 interpreter. However, it’s fair to say that all the practical examples that you saw in the previous installment of the series weren’t capable of triggering these exceptions in a way that can be caught by the “catch()” block.

Nonetheless, this issue can be fixed with relative ease. That will be the goal of this third article of the series. So if you’re interested in learning how this solution will be implemented, don’t waste any more time and start reading now!

{mospagebreak title=One Step Backward}

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()){

if(!is_array($options)){

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

}

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

if(empty($option)){

throw new Exception(‘Connection parameter cannot be empty’);

}

$this->{$option}=$value;

}

$this->connectDb();

}

// private ‘connectDb()’ method

private function connectDb(){

if(!$this->connId=mysql_connect($this->host,$this->user,$this->password)){

throw new Exception(‘Error connecting to MySQL’);

}

if(!mysql_select_db($this->database,$this->connId)){

throw new Exception(‘Error selecting database’);

}

}

// public ‘query()’ method

public function query($sql){

if(!$result=mysql_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){

$this->mysql=$mysql;

$this->result=$result;

}

// public ‘fetch()’ method

public function fetch(){

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

}

// public ‘count()’ method

public function count(){

if(!$rows=mysql_num_rows($this->result)){

throw new Exception(‘Error counting rows’);

}

return $rows;

}

// public ‘get_insertId()’ method

public function getInsertId(){

if(!$insId=mysql_insert_id($this->mysql->connId)){

throw new Exception(‘Error getting insert ID’);

}

return $insId;

}

// public ‘seek()’ method

public function seek($row){

if(!int($row)&&$row<0){

throw new Exception(‘Invalid row parameter’);

}

if(!$row=mysql_data_seek($this->mysql->connId,$row)){

throw new Exception(‘Error seeking row’);

}

return $row;

}

// public ‘getAffectedRows()’ method

public function getAffectedRows(){

if(!$rows=mysql_affected_rows($this->mysql->connId)){

throw new Exception(‘Error counting affected rows’);

}

return $rows;

}

}



(definition of autoload_example.php file)


 

try{

function __autoload($className){

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

eval("class $className {}");

throw new Exception(‘Class 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();

exit();

}


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.

{mospagebreak title=Improving the Definition}

Improving the definition of the “__autoload()” function: throwing exceptions that can be caught by a “try-catch” block


In consonance with the concepts deployed in the prior section, it’s necessary to modify the initial definition of the “__autoload()” magic function so that it can throw an exception when it fails to include a determined source class into the code. In this case, the exception should be caught by a “try-catch()” block. It’s an extremely handy technique for taking advantage of the built-in exception mechanism provided by PHP 5.

So, keeping this in mind, below I listed the improved definition of the “__autoload()” function, which, at this point, is capable of triggering exceptions that can be easily caught by a “try-catch()” block.

Having said that, the modified signature of this handy function is as follows:


function __autoload($className){

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

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

}

require_once $className.’.php';

}


See how easy it is to change the definition of the “__autoload()” PHP 5 function to make it throw exceptions that can be correctly intercepted within a “try-catch()” structure? I guess you do! In this particular case, I implemented the function in such a way that when it fails to include a source class, it first declares this class, then an exception is triggered by the corresponding constructor, and finally, the class’ code is returned to the program’s flow.

Of course, I should say that this workaround is quite dirty and not very elegant. But you should keep in mind that I’m trying to implement a feature that isn’t supported natively by the "__autoload()” function (i.e. triggering exceptions that can be caught within a “try-catch()” block).

So far, so good. At this stage I’ve demonstrated how to modify the signature of the “__autoload()” function in order to add to it a decent exception support. However, if you’re anything like me, then you may want to see for yourself whether this improved incarnation of the function really works as expected.

Thus, in the last section of this tutorial I’m going to create a fully functional example that will demonstrate how the “__autoload()” function is now capable of throwing exceptions that can be intercepted within a “try-catch()” block.

To learn how this example will be developed, go ahead and read the next few lines. They’re just one click away.

{mospagebreak title=The Improved Source Code}

Putting all the pieces together: listing the improved source code of the sample database-driven application

As I explained in the previous section, below I’ve included the definitions of all the source files that make up the sample database-driven application you learned in the beginning of this article. This time they include the modified version of the  “__autoload()” function.

Here are the files:


(definition of mysql.php file)


class MySQL{

private $host;

private $user;

private $password;

private $database;

private $connId;

// constructor

function __construct($options=array()){

if(!is_array($options)){

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

}

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

if(empty($option)){

throw new Exception(‘Connection parameter cannot be empty’);

}

$this->{$option}=$value;

}

$this->connectDb();

}

// private ‘connectDb()’ method

private function connectDb(){

if(!$this->connId=mysql_connect($this->host,$this->user,$this->password)){

throw new Exception(‘Error connecting to MySQL’);

}

if(!mysql_select_db($this->database,$this->connId)){

throw new Exception(‘Error selecting database’);

}

}

// public ‘query()’ method

public function query($sql){

if(!$result=mysql_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){

$this->mysql=$mysql;

$this->result=$result;

}

// public ‘fetch()’ method

public function fetch(){

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

}

// public ‘count()’ method

public function count(){

if(!$rows=mysql_num_rows($this->result)){

throw new Exception(‘Error counting rows’);

}

return $rows;

}

// public ‘get_insertId()’ method

public function getInsertId(){

if(!$insId=mysql_insert_id($this->mysql->connId)){

throw new Exception(‘Error getting insert ID’);

}

return $insId;

}

// public ‘seek()’ method

public function seek($row){

if(!int($row)&&$row<0){

throw new Exception(‘Invalid row parameter’);

}

if(!$row=mysql_data_seek($this->mysql->connId,$row)){

throw new Exception(‘Error seeking row’);

}

return $row;

}

// public ‘getAffectedRows()’ method

public function getAffectedRows(){

if(!$rows=mysql_affected_rows($this->mysql->connId)){

throw new Exception(‘Error counting affected rows’);

}

return $rows;

}

}



(definition of autoload_example.php file)


try{

function __autoload($className){

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

return eval("class {$className}{public function __construct(){throw new
Exception(‘Class 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();

exit();

}


Now, having listed all of the source files that comprise the previous database-driven application, suppose that for some reason the file containing the definition of the prior “MySQL” class is simply not available. Based on this situation, when the above “autoload_sample.php” file is parsed by the PHP 5 interpreter, the “__autoload()” magic function will trigger an exception, which will then be caught by the corresponding “catch()” statement and the following error message will be displayed on the browser:


Class not found!


Pretty good, right? At this stage I’ve shown you how to define the “__autoload()” function in order to provide it with the capacity to trigger exceptions that can be completely intercepted by a conventional “try-catch()” block.

As usual, with many of my articles about PHP development, I encourage you to tweak the code of all the examples developed in this tutorial. Doing this will give you more practice with using the handy “__autoload()” function. Start automatically loading your own PHP 5 source classes.

Final thoughts

In this third tutorial of the series I showed you how to tweak the signature of the “__autoload()” PHP 5 magic function in order to give it the capacity to throw exceptions when a particular source class can’t be included into the client code. Of course, the most notable result of improving the definition of this function is that those exceptions can be perfectly caught within a conventional “try-catch()” block, taking advantage of the exceptions mechanism natively provided by PHP 5.

In the next (and last) part of the series, I’m going to demonstrate how to make the “__autoload()” function even more efficient and elegant by improving the way it throws all eventual exceptions.

Now that you’re aware of the subject of the next article, you simply can’t miss it!

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort