Created
June 9, 2016 15:50
-
-
Save NiltonMorais/d5e9d454d603df6a0db39f08798804af to your computer and use it in GitHub Desktop.
Autoload Example
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 | |
| define('CLASS_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR); | |
| set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR); | |
| spl_autoload_register(); |
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 | |
| define('CLASS_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR); | |
| set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR); | |
| spl_autoload_register(function($className) { | |
| $path = str_replace('\\', DIRECTORY_SEPARATOR, $className); | |
| $file = CLASS_DIR . $path . '.php'; | |
| if (is_file($file)) { | |
| require_once($file); | |
| } else { | |
| throw new \ErrorException("Could not load class {$className}. File not found: {$file}"); | |
| die(); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment