Skip to content

Instantly share code, notes, and snippets.

@gabrieledarrigo
Created June 7, 2013 08:22
Show Gist options
  • Save gabrieledarrigo/5727816 to your computer and use it in GitHub Desktop.
Save gabrieledarrigo/5727816 to your computer and use it in GitHub Desktop.
Singleton Pattern applied to a Service of Zend Framework
<?php
/**
* This service return an instance of Zend Cache Manager Plugin.
*
* @author gdarrigo
*/
class Application_Service_MemcacheRetriever {
protected static $_istance = null;
protected $_cache;
/**
* Initialize Zend Cache Manager.
*/
private function __construct() {
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
if ($bootstrap->hasPluginResource('cachemanager')) {
$cacheManager = $bootstrap->getPluginResource('cachemanager');
$this->_cache = $cacheManager->getCacheManager()->getCache('memcached');
}
}
/**
* Prevent Singleton to be cloned.
*
* @return boolean
*/
private function __clone() {
return new Exception('You can\'t clone a Singleton');
}
/**
* Retrieve an istance of singleton.
*
* @return type
*/
public static function getIstance() {
if (self::$_istance === null) {
self::$_istance = new self();
}
return self::$_istance;
}
/**
* Return Zend Cache Manager.
*
* @return type
*/
public function getCache() {
return $this->_cache;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment