diff --git a/README.md b/README.md index 0eaf239..150f869 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,16 @@ Database connections are set in /app/config/db.php * Loading a class - Load a system library, or a custom library from the `app/classes` folder. - - `$this->load_class($class)` - creates an instance of that class as a member of the current class. After loading the class, you can call its methods like so + Librarys / classes found in `app/classes` or `sys/libraries` are autoloaded. + To call a library, simply instantiate that class. - `$this->[class name]->method()` \ No newline at end of file + Classes with a `get_instance` static methods should be called like so: + + `$obj =& class::get_instance()` + + + Other classes should be called using the new operator + + `$obj = new class()` + + \ No newline at end of file diff --git a/sys/common.php b/sys/common.php index ea47a17..7f767e3 100644 --- a/sys/common.php +++ b/sys/common.php @@ -24,6 +24,41 @@ // ! Error handling / messages // -------------------------------------------------------------------------- +/** + * Function to autoload libraries + * + * @param string + */ +function autoload($name) +{ + // In a subdirectory? No problem + if (strpos("/", $name) !== FALSE) + { + $n = explode("/", $name); + $name = $n[count($n) -1]; + } + + // Try system library first, then app library + $name = strtolower($name); + $path = MM_SYS_PATH . "libraries/{$name}.php"; + + if ( ! is_file($path)) + { + $path = MM_APP_PATH . "classes/{$name}.php"; + } + + // Load the class file if need be + if ( ! class_exists($name)) + { + if (is_file($path)) + { + require_once($path); + } + } +} + +// -------------------------------------------------------------------------- + /** * Function to run on script shutdown * -used to catch most fatal errors, and @@ -272,6 +307,9 @@ function init() require_once(MM_SYS_PATH . 'core/miniMVC.php'); array_map('do_include', glob(MM_SYS_PATH . 'core/*.php')); + // Start the library autoloader + spl_autoload_register('autoload'); + // Map to the appropriate module/controller/function route(); } diff --git a/sys/core/miniMVC.php b/sys/core/miniMVC.php index f4142b0..55e1cc8 100644 --- a/sys/core/miniMVC.php +++ b/sys/core/miniMVC.php @@ -36,63 +36,6 @@ class miniMVC extends MM { // -------------------------------------------------------------------------- - /** - * Method to load classes into the singleton - * - * @param string $name - * @return object - */ - public function load_class($name) - { - // In a subdirectory? No problem - if (strpos("/", $name) !== FALSE) - { - $n = explode("/", $name); - $name = $n[count($n) -1]; - } - - // Try system library first, then app library - $path = MM_SYS_PATH . "libraries/{$name}.php"; - $name = "MM_{$name}"; - - if ( ! is_file($path)) - { - $path = MM_APP_PATH . "classes/{$name}.php"; - $name = str_replace('MM_', '', $name); - } - - $class = "{$name}"; - - // Load the class file if need be - if ( ! class_exists($class, FALSE)) - { - if (is_file($path)) - { - require_once($path); - } - } - - // Create the object, and add it to the current miniMVC object - if (class_exists($class, FALSE)) - { - // Remove MM_ prefix from name - $name = str_replace('MM_', '', $name); - - if ( ! isset($this->$name)) - { - // Call a singleton, if the get_instance method exists - if (method_exists($class, 'get_instance')) - { - return $this->$name =& $class::get_instance(); - } - - return $this->$name = new $class; - } - } - } - - // -------------------------------------------------------------------------- - /** * Convenience function to remove an object from the singleton * diff --git a/sys/core/page.php b/sys/core/page.php index 8bd5fef..d57fab1 100644 --- a/sys/core/page.php +++ b/sys/core/page.php @@ -110,6 +110,16 @@ class MM_Page extends MM_Output { // -------------------------------------------------------------------------- + /** + * call the parent destructor + */ + public function __destruct() + { + parent::__destruct(); + } + + // -------------------------------------------------------------------------- + /** * Sets server headers and doctype * diff --git a/sys/libraries/data_store.php b/sys/libraries/data_store.php index 0a96971..8a44ade 100644 --- a/sys/libraries/data_store.php +++ b/sys/libraries/data_store.php @@ -19,7 +19,7 @@ * @package miniMVC * @subpackage Libraries */ -class MM_Data_Store { +class Data_Store { /** * Settings object represented by the currently loaded JSON file diff --git a/sys/libraries/session.php b/sys/libraries/session.php index bdf3ba7..3693188 100644 --- a/sys/libraries/session.php +++ b/sys/libraries/session.php @@ -19,7 +19,7 @@ * @package miniMVC * @subpackage Libraries */ -class MM_Session { +class Session { // Combine traits for more dynamic behavior // but use Singleton for __invoke, so calling