Auto Loading Classes in PHP 5 - Comparing the two (Page 5 of 5 )
Comparing two different approaches for loading source classes: listing the previous code samples
As I promised in the previous section, here is the complete source code corresponding to the two examples that you learned earlier, where the first one uses conventional PHP includes to load the pertinent “MySQL” and “Result” classes, and the second one utilizes the handy “__autoload()” function:
(example using conventional PHP includes)
try{
// include required classes
require_once 'mysql.php';
require_once 'result.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();
}
// example using the magic '__autoload()' function
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();
}
From this point on, it’s up to you decide which approach fits your specific needs better when it comes to loading all the source classes required by a specific PHP 5 application. I’ve been using the “__autoload()” magic function for one year or so, and I consider it a superior option over common PHP includes.
Final thoughts
In this initial article of the series, I introduced the basics of using the “__autoload()” magic function that comes bundled with PHP 5. As you saw, it can be really useful for loading automatically all the source classes required by a given application.
However, this function has an important downside, since any exception thrown when attempting to load a concrete class simply can’t be caught inside a “try-catch” block. However, there are some workarounds that come in handy for solving this issue. And I’ll be showing them in the next part of the series.
Now that you’ve been warned, you won’t want to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |