miniMVC/sys/core/traits/Singleton.php

81 lines
1.4 KiB
PHP
Raw Normal View History

2012-05-21 14:58:58 -04:00
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
2012-05-23 12:01:13 -04:00
* @link https://github.com/aviat4ion/miniMVC
2012-05-21 14:58:58 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
2012-05-22 11:11:36 -04:00
namespace miniMVC;
2012-05-21 14:58:58 -04:00
/**
* Singleton pattern
*
* @package miniMVC
* @subpackage System
*/
trait Singleton {
use Generic;
/**
* Singleton object
*
* @var self
*/
protected static $instance;
/**
* Protected constructor for creating the one instance
*/
abstract protected function __construct();
/**
* PHP magic method that is called when an object is treated as a function
*
* @param array $params
* @return self
*/
public static function __invoke($params = [])
{
return self::get_instance($params);
}
// --------------------------------------------------------------------------
/**
* Singleton getter function
*
* @return self
*/
public static function &get_instance()
{
if ( ! isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
// --------------------------------------------------------------------------
/**
* Magic function called when cloning an object
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
// End of Singleton.php