Created
December 2, 2024 18:30
-
-
Save deltafactory/ef5364235a0fb34532ae62b611351689 to your computer and use it in GitHub Desktop.
Dynamic PHP class autoloader based on namespace and filename
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 | |
spl_autoload_register( function( $class_name ) { | |
$prefix = 'NAMESPACE'; | |
if ( 0 !== strpos( $class_name, $prefix ) ) { | |
return; | |
} | |
$class = substr( str_replace( "\\", '/', $class_name ), strlen( $prefix ) + 1 ); | |
$path = __DIR__ . '/include/' . $class . '.php'; | |
if ( is_readable( $path ) ) { | |
include( $path ); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment