PHP
  Home arrow PHP arrow Page 3 - Auto Loading Classes in PHP 5
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Auto Loading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 10
    2007-12-04

    Table of Contents:
  • Auto Loading Classes in PHP 5
  • Traditional Approach
  • Traditional Approach continued
  • Using the Magic Function
  • Comparing the two

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Dell PowerEdge Servers

    Auto Loading Classes in PHP 5 - Traditional Approach continued
    (Page 3 of 5 )

    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


       · Ever wanted to build PHP 5 scripts that load automatically your source classes...
       · Yeah I Used This For A While. But Notice That If You Have Some Variables Or...
       · Thank you for commenting on my PHP article. Yes, what you mention is a cons in using...
       · You did not say (or I missed it) what happens if you use both methodologies. If a...
       · Thank you for commenting on my PHP article. Concerning your question, simply I...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway