php类的静态化实例,静态化实例可避免重复new一个类

php类的静态化实例,静态化实例可避免重复new一个类
提高程序效率

<?php
class Cart {
	private static $instance;
	private $test;

	public function __construct() {
		echo "n输出几次就实例了几次n";
	}

	static public function instance() {
		if (! self::$instance) {
			self::$instance = new Cart ();
		}
		return self::$instance;
	}

	public function go() {
		$this->test = 'I`m a private var';
	}

	public function output() {
		echo "<br>";
		print_r ( $this->test );
		echo "<br />";
	}
}

$cart = Cart::instance ();
$cart->go ();
$cart->output ();

$cart = Cart::instance ();
$cart->output ();