Skip to content

Instantly share code, notes, and snippets.

@NiltonMorais
Created June 9, 2016 15:50
Show Gist options
  • Select an option

  • Save NiltonMorais/d5e9d454d603df6a0db39f08798804af to your computer and use it in GitHub Desktop.

Select an option

Save NiltonMorais/d5e9d454d603df6a0db39f08798804af to your computer and use it in GitHub Desktop.
Autoload Example
<?php
define('CLASS_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR);
set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR);
spl_autoload_register();
<?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