PHP根据当前类型的单例模式

class Model_Table extends Zend_Db_Table_Abstract {
	private static $instances = array ();
	static function getInstance($config = array()) {
		$key = get_called_class ();
		if (! array_key_exists ( $key, self::$instances )) {
			self::$instances [$key] = new static ( $config );
		}
		return self::$instances [$key];
	}
}

 

class Pass_Table_UserIdModel extends Model_Table_Pass {
	protected $_name = 'user_id';
	protected $_primary = 'username';
	protected $_dependentTables = array (
			'Pass_Table_UserModel' 
	);
}

 

$userid = Pass_Table_UserIdModel::getInstance ();
vardump($userid);

$userid的类型就是Pass_Table_UserIdModel,关键的地方是new static($config),这个是后期静态绑定

参考:

http://php.net/manual/zh/language.oop5.late-static-bindings.php

发表评论

电子邮件地址不会被公开。

*