Home arrow PHP arrow Page 5 - Auto Loading Classes in PHP 5

Comparing the two - PHP

Undoubtedly, the release of PHP 5 has had a remarkable impact on the way that object-oriented applications are developed nowadays. This highly-improved model has provided PHP programmers with features that were only present in mature object-based languages, like Java and C++, but now, fortunately for you and me, they are generously offered by this powerful server-side scripting language.

TABLE OF CONTENTS:
  1. Auto Loading Classes in PHP 5
  2. Traditional Approach
  3. Traditional Approach continued
  4. Using the Magic Function
  5. Comparing the two
By: Alejandro Gervasio
Rating: starstarstarstarstar / 21
December 04, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



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