register(); * * // register the base directories for the namespace prefix * $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src'); * $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests'); * * The following line would cause the autoloader to attempt to load the * \Foo\Bar\Qux\Quux classe from /path/to/packages/foo-bar/src/Qux/Quux.php: * * debug = $debug; } private function affichageDebogage(string $message) : void { if ($this->debug) echo $message; } /** * An associative array where the key is a namespace prefix and the value * is an array of base directories for classes in that namespace. * * @var array */ protected array $prefixes = array(); /** * Register loader with SPL autoloader stack. * * @return void */ public function register() : void { spl_autoload_register(array($this, 'loadClass')); } /** * Adds a base directory for a namespace prefix. * * @param string $prefix The namespace prefix. * @param string $base_dir A base directory for classe files in the * namespace. * @param bool $prepend If true, prepend the base directory to the stack * instead of appending it; this causes it to be searched first rather * than last. * @return void */ public function addNamespace(string $prefix, string $base_dir, bool $prepend = false) : void { // normalize namespace prefix $prefix = trim($prefix, '\\') . '\\'; // normalize the base directory with a trailing separator $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; // initialize the namespace prefix array if (isset($this->prefixes[$prefix]) === false) { $this->prefixes[$prefix] = array(); } // retain the base directory for the namespace prefix if ($prepend) { array_unshift($this->prefixes[$prefix], $base_dir); } else { array_push($this->prefixes[$prefix], $base_dir); } } /** * Loads the classe file for a given classe name. * * @param string $class The fully-qualified classe name. * @return mixed The mapped file name on success, or boolean false on * failure. */ public function loadClass(string $class) { $this->affichageDebogage("
Réussite : fichier chargé
$file"); require $file; return true; } $this->affichageDebogage("
Échec : fichier introuvable
$file"); return false; } }