diff --git a/README.md b/README.md index a514479..98361c4 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,7 @@ UserApp relies on the autoloading features of PHP to load its files when needed. #### Not using Composer? Use the library's own autoloader - require 'lib/Autoloader.php'; - UserApp\Autoloader::register(); + require 'autoload.php'; ### Creating your first client diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..f38dfba --- /dev/null +++ b/autoload.php @@ -0,0 +1,5 @@ +directory = $baseDirectory; - $this->prefix = __NAMESPACE__ . '\\'; - $this->prefixLength = strlen($this->prefix); - } - - public static function register($prepend = false) - { - spl_autoload_register(array(new self, 'autoload'), true, $prepend); - } - - public function autoload($className) - { - if (0 === strpos($className, $this->prefix)) { - $parts = explode('\\', substr($className, $this->prefixLength)); - $filepath = $this->directory.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts).'.php'; - - if (is_file($filepath)) { - require($filepath); - } - } - } - } - -?> \ No newline at end of file diff --git a/lib/UserApp/Autoloader.php b/lib/UserApp/Autoloader.php new file mode 100644 index 0000000..e154f1c --- /dev/null +++ b/lib/UserApp/Autoloader.php @@ -0,0 +1,47 @@ +directory = $baseDirectory; + $this->prefix = __NAMESPACE__ . '\\'; + $this->prefixLength = strlen($this->prefix); + } + + /** + * Registers the autoloader class with the PHP SPL autoloader. + * + * @param bool $prepend Prepend the autoloader on the stack instead of appending it. + */ + public static function register($prepend = false) + { + spl_autoload_register(array(new self, 'autoload'), true, $prepend); + } + + /** + * Loads a class from a file using its fully qualified name. + * + * @param string $className Fully qualified name of a class. + */ + public function autoload($className) + { + if (0 === strpos($className, $this->prefix)) { + $parts = explode('\\', substr($className, $this->prefixLength)); + $filepath = $this->directory.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts).'.php'; + + if (is_file($filepath)) { + require($filepath); + } + } + } +} \ No newline at end of file