miniMVC/sys/core/traits/singleton.php

81 lines
1.4 KiB
PHP

<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/timw4mail/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace miniMVC;
/**
* 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