Created
April 12, 2015 04:40
-
-
Save lishiyo/c886ca4491daf6484b3e to your computer and use it in GitHub Desktop.
PHP Examples - Autoload
This file contains 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
/* | |
Autoloading is very nifty, and is used in a lot of large frameworks - it allows you to load the file that a class is written in automatically when you try to instantiate it. The advantage of this is that only classes that you want to instantiate & use will be included in your page and will keep memory usage down. | |
*/ | |
// Using an anonymous function | |
spl_autoload_register(function($class){ | |
include_once('includes/' . $class . '.class.php') | |
}); | |
// Using a named function | |
function autoload_class($class){ | |
include_once('includes/' . $class . '.class.php'); | |
} | |
spl_autoload_register('autoload_class'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment