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

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

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
 

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: