MarkJane
4/10/2019 - 2:04 PM

基类扩展

<?php
class ObjectUtil
{
	static $instances;

	static public function singleton($className, $autoInited = true)
	{
		if (empty($className))
		{
			trigger_error('empty class name');
			return false;
		}
		$key = strtolower($className);
		if (isset(self::$instances[$key]))
		{
			return self::$instances[$key];
		}
		else if (class_exists($className))
		{
			$newInstance = new $className;
			if ($autoInited && method_exists($newInstance, 'init'))
			{
				$newInstance->init();
			}
			self::$instances[$key] = $newInstance;
			return $newInstance;
		}
		else
		{
			return false;
		}
	}
}