Created
August 31, 2012 21:59
-
-
Save BaylorRae/3559742 to your computer and use it in GitHub Desktop.
Source code for: http://baylorrae.com/lazy-loading-with-php/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Allows multiple __autoload stacks | |
// and keeps autoloader in Huck class | |
spl_autoload_register(array('Huck', 'autoload')); | |
class Huck { | |
public static function autoload($class_name) { | |
if( substr($class_name, 0, 5) === 'Huck_' ) { | |
$file = dirname(__FILE__) . '/' . substr($class_name, 5) . '.php'; | |
if( file_exists($file) ) | |
require_once $file; | |
} | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function __autoload($class_name) { | |
// looks for 'classes/user.php'; | |
$file = 'classes/' . strtolower($class_name) . '.php'; | |
if( file_exists($file) ) | |
require_once $file; | |
} | |
/* | |
Because the User class has not been included on this page | |
PHP will run `__autoload` and try to include it automatically | |
*/ | |
$user = new User; | |
// do stuff with $user | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment