Skip to content

Instantly share code, notes, and snippets.

@deltafactory
Created December 2, 2024 18:30
Show Gist options
  • Save deltafactory/ef5364235a0fb34532ae62b611351689 to your computer and use it in GitHub Desktop.
Save deltafactory/ef5364235a0fb34532ae62b611351689 to your computer and use it in GitHub Desktop.
Dynamic PHP class autoloader based on namespace and filename
<?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