PHP
  Home arrow PHP arrow Page 2 - 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 
 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

    PCmover - $15 Off with Coupon Code CJPH7Q

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


    Loading PHP 5 classes using a traditional approach: developing an illustrative example

    An adequate place to start demonstrating how the “__autoload()” PHP 5 magic function works is by developing a simple database-driven application, where all of the classes required by the application in question are loaded into client code by using a couple of “require_once()” functions.

    By creating this “conventional” sample application, you’ll have at your disposal an excellent point of reference for comparing the pros and cons of using this approach with utilizing one that implements the mentioned “__autoload()” function.

    Having clarified this point, suppose that there’s a MySQL database table that has been populated with some basic data about a few fictional users, which needs to be displayed on the browser. To perform this process as easily as possible, I’ll use two different MySQL handling classes that will reside in different files.

    In this case, the first class, called “MySQL,” was previously stored on a “mysql.php” file. It has the following signature:


    class MySQL{

    private $host;

    private $user;

    private $password;

    private $database;

    private $connId;

    // constructor

    function __construct($options=array()){

    if(!is_array($options)){

    throw new Exception('Connection options must be an array');

    }

    foreach($options as $option=>$value){

    if(empty($option)){

    throw new Exception('Connection parameter cannot be empty');

    }

    $this->{$option}=$value;

    }

    $this->connectDb();

    }

    // private 'connectDb()' method

    private function connectDb(){

    if(!$this->connId=mysql_connect($this->host,$this->user,$this->password)){

    throw new Exception('Error connecting to MySQL');

    }

    if(!mysql_select_db($this->database,$this->connId)){

    throw new Exception('Error selecting database');

    }

    }

    // public 'query()' method

    public function query($sql){

    if(!$result=mysql_query($sql)){

    throw new Exception('Error running query '.$sql.' '.mysql_error());

    }

    return new Result($this,$result);

    }

    }


    You might find the definition of the above “MySQL” class quite familiar, since I used it in some of my previous articles on PHP development. As you can see, this class defines a group of methods for connecting to the pertinent database server, as well as for performing queries against one or more tables. So far, the signature of this class is not rocket science at all.

    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 4 hosted by Hostway