<

Autoload function in PHP5

Published on
6,259 Points
3,259 Views
Last Modified:
As you all know PHP got one problem, before using other files classes or functions you need to include or use require function. But now in PHP 5 there's solution for this, it's called __autoload() function. You can read more about function in PHP manual http://ee.php.net/manual/en/function.spl-autoload.php.

What would be best way to use this, as you have seen the usage in Zend Framework, they have got really simple way to do this. All the class names contain the path in names. Like it would be: My_Database_Mysql so this means it's located in folder My/Database/Mysql.php. You could use your own delimiter (- . :).

As the zip with php files isn't allowed to upload so explain the scheme of directories.
/library/Database/Mysql/Pdo.php
/library/Database/Mysql/Mysqli.php
/library/Database/Mysql/Default.php

/library/Database/Oracle/Default.php
 
<?php
set_include_path('./library');

function __autoload($className) {
	$path = str_replace('_', '/', $className);
	require_once("{$path}.php");
}
$pdo = new Database_Mysql_Pdo();
var_dump($pdo);

$mysqli = new Database_Mysql_Mysqli();
var_dump($mysqli);
?>

Open in new window

0
Comment
Author:staarland
1 Comment
 
 

Administrative Comment

by:Richard Quadling
Still linking to the wrong URL.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Join & Write a Comment

Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month