From 6a7a5cc0da40020fa48e9bed8fbb60b28905db99 Mon Sep 17 00:00:00 2001 From: Robin Orheden Date: Wed, 9 Jul 2014 18:07:12 +0200 Subject: [PATCH] Fixed manual autoloader. --- README.md | 3 +-- autoload.php | 5 ++++ lib/Autoloader.php | 36 ----------------------------- lib/UserApp/Autoloader.php | 47 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 autoload.php delete mode 100644 lib/Autoloader.php create mode 100644 lib/UserApp/Autoloader.php 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