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.

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

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

$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;

}

}


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:


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

}


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
   

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: