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

Traditional Approach continued - 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.

  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



However, the point to stress in this case is that the prior class resides in a separate file, called “mysql.php,” which should be included manually into this sample application via a "require_once()" PHP function. Also, to make the application work as expected, I’m going to define an additional class, called “Result,” which will reside in a different file called “result.php.” It will look like this:

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;



As you can see, the above “Result” class comes in handy for processing MySQL result-sets in all sorts of clever ways. And it’s capable of doing some interesting things, like fetching and counting the rows returned in a data set, determining the ID of a particular insert operation, etc. It's nothing too complex, actually.

Besides, as I said before, this class is stored in a “result.php” file, which should be included in this sample database-driven application via a “require_once()” function, as you’ve possibly done hundreds of times before.

So, having showed you the respective definitions of the two previous MySQL handling classes, I want to fetch a few database records from a basic “Users” table and display them on the browser. The following script should do the job quite decently:


// include required classes

require_once 'mysql.php';

require_once 'result.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();



In this case, the above script will display some basic data about a few fictional users, including their first and last names, by using the pair of MySQL processing classes that you learned before. Nonetheless, the most important thing to highlight here is that the classes are loaded “manually” by using the popular “require_once()” PHP native function. This is a process that should be familiar to you.

So far, I've recreated a hypothetical situation where the classes required by a certain database-driven application are included manually into client code via a couple of “require_once()” functions.

Now that you've seen how this simple process is achieved, it’s time to take a close look at the handy “__autoload()” PHP 5 magic function. As you’ll see in a few moments, it will allow you to load the previous classes “automatically" and logically without having to use any “require_once()” statements.

To learn how this handy function will be used in the context of the prior database-driven application, 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: