Autoload function in PHP5

Published on
6,405 Points
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.


function __autoload($className) {
	$path = str_replace('_', '/', $className);
$pdo = new Database_Mysql_Pdo();

$mysqli = new Database_Mysql_Mysqli();

Open in new window

Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free