Created
June 7, 2013 08:22
-
-
Save gabrieledarrigo/5727816 to your computer and use it in GitHub Desktop.
Singleton Pattern applied to a Service of Zend Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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