Tuesday, August 11, 2009

Lazy Loading to improve performance of your php code


Share/Bookmark
See above example code, or event you do like that :D :

<?php
require '/class/Database.php';
require '/class/Common.php';
require '/class/Action.php';
require '/class/Template.php';
require '/class/Module.php';

?>


It's not good for server because php engine must load every file when execute, in case of it's not necessary.

<?php
define('CLASS_DIR', 'class/')
set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR);
spl_autoload_extensions('.class.php');
spl_autoload_register();

function autoload($className)
{
include_once($className);
}


$user = new user();
?>

This message is quoted from "zend certification study guide" :">
"By default, SPL uses its own autoloader, called spl_autoload(); this built-in
function checks all include paths for filenames that match the name of the class
that needs loading in lowercase letters, followed by .inc, .php, or the exten-
sions specified using a comma-separated string as the only parameter to a call to
spl_autoload_extensions()"

Regard to function spl_autoload_extensions(), it allow you to define extension of filename.