Removed in favor of autoloading

This commit is contained in:
Timothy Warren 2012-05-21 14:29:36 -04:00
parent 6708888579
commit e434c0d865
6 changed files with 62 additions and 63 deletions

View File

@ -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()`
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()`

View File

@ -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();
}

View File

@ -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
*

View File

@ -110,6 +110,16 @@ class MM_Page extends MM_Output {
// --------------------------------------------------------------------------
/**
* call the parent destructor
*/
public function __destruct()
{
parent::__destruct();
}
// --------------------------------------------------------------------------
/**
* Sets server headers and doctype
*

View File

@ -19,7 +19,7 @@
* @package miniMVC
* @subpackage Libraries
*/
class MM_Data_Store {
class Data_Store {
/**
* Settings object represented by the currently loaded JSON file

View File

@ -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