A basic PHP autoloader setup
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
<?php | |
/** | |
* In this example, we are autoloading classes within the namespace `Acme\ExampleProject`. The file `index.php` is the | |
* entry point to our project; and we are using the `src` directory to store and organize our PHP class files. Our | |
* autoloader is located in `autoload.php` | |
* | |
* The basic behavior is illustrated in the fact that PHP will autoload the file `src/mammals/human.php` whenever we | |
* attempt to create a `new \Acme\ExampleProject\Mammals\Human` anywhere in our project's codebase. | |
* | |
* This diagram illustrates our project structure. Each file's contents can be found below. | |
* | |
* - /project-root | |
* - | |
* - /autoload.php | |
* - /index.php | |
* - | |
* - /src | |
* - /mammals | |
* - /human.php | |
* | |
* This example does not consider all complications for all use cases and does not necessarily conform to any standard. | |
* For more information on standards and best practices, see PHP Standards Recommendations. | |
* | |
* @link https://www.php-fig.org/psr/ | |
*/ |
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
<?php | |
/** | |
* This file is the entry point to our project | |
*/ | |
require_once __DIR__ . '/autoload.php'; | |
$john_doe = new \Acme\ExampleProject\Mammals\Human; | |
var_dump( $john_doe ); |
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
<?php | |
namespace Acme\ExampleProject\Mammals; | |
class Human { | |
/** etc **/ | |
} |
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
<?php | |
/** | |
* If the $class being instantiated is within the namespace `Acme\ExampleProject`, then find the correct | |
* file within our `src` directory | |
*/ | |
spl_autoload_register( function( $class ) { | |
# namespace prefix that we will use for autoloading the appropriate classes and ignoring others | |
$prefix = 'Acme\\ExampleProject\\'; | |
# base directory where our project's files reside | |
$base_dir = __DIR__ . '/src/'; | |
/** | |
* Does the class being called use our specific namespace prefix? | |
* | |
* - Compare the first {$len} characters of the class name against our prefix | |
* - If no match, move to the next registered autoloader in the system (if any) | |
*/ | |
# character length of our prefix | |
$len = strlen( $prefix ); | |
# if the first {$len} characters don't match | |
if ( strncmp( $prefix, $class, $len ) !== 0 ) { | |
return; | |
} | |
# get the name of the class after our prefix has been peeled off | |
$class_name = str_replace( $prefix, '', $class ); | |
/** | |
* Perform normalizing operations on the namespace/class string in order to get the file name to be required | |
* | |
* - Replace namespace separators with directory separators in the class name | |
* - Prepend the base directory | |
* - Append with .php | |
*/ | |
$file = $base_dir . str_replace('\\', '/', $class_name ) . '.php'; | |
# require the file if it exists | |
if( file_exists( $file ) ) { | |
require $file; | |
} | |
}); # end: spl_autoload_register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment