includes/Foo/Bar.php. * * @package GSFP */ namespace GSFP\Core; if ( ! defined( 'ABSPATH' ) ) { exit; } final class Autoloader { const PREFIX = 'GSFP\\'; /** @var string */ private static $base = ''; public static function register( $base ) { self::$base = rtrim( $base, '/\\' ) . '/'; spl_autoload_register( array( __CLASS__, 'load' ) ); } public static function load( $class ) { if ( 0 !== strpos( $class, self::PREFIX ) ) { return; } $relative = substr( $class, strlen( self::PREFIX ) ); // Only allow namespace-safe characters; never build paths from arbitrary input. if ( ! preg_match( '/^[A-Za-z0-9_\\\\]+$/', $relative ) ) { return; } $file = self::$base . str_replace( '\\', '/', $relative ) . '.php'; if ( is_readable( $file ) ) { require_once $file; } } }