Skip to content

Commit

Permalink
refs #1609 : new singleton implementation added for PHP 5.3/4.
Browse files Browse the repository at this point in the history
  • Loading branch information
inureyes committed Oct 3, 2012
1 parent 1921b98 commit 1367660
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
66 changes: 50 additions & 16 deletions framework/boot/10-CoreClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,62 @@
/// See the GNU General Public License for more details. (/documents/LICENSE, /documents/COPYRIGHT)

/// Singleton implementation.
abstract class Singleton {
private static $instances = array();

protected function __construct() {
if(version_compare(PHP_VERSION, '5.3.0','>=')) { // >= 5.3 Singleton implementation
class Singleton {
// If your model support higher than PHP 5.3, you do not implement getInstance method.
// However, models for prior to PHP 5.3 should have getInstance method.
// See below (<5.3 Compatible singleton implementation)
private static $instances;

public function __construct() {
$c = get_class($this);
if(isset(self::$instances[$c])) {
throw new Exception('You can not create more than one copy of a singleton.');
} else {
self::$instances[$c] = $this;
}
}
public static function _getInstance($p = null) {
$c = get_called_class();
if (!isset(self::$instances[$c])) {
$args = func_get_args();
$reflection_object = new ReflectionClass($c);
self::$instances[$c] = $reflection_object->newInstanceArgs($args);
}
return self::$instances[$c];
}
public static function getInstance() {
return self::_getInstance();
}
public function __clone() {
throw new Exception('You can not clone a singleton.');
}
}
} else { // < 5.3 Compatible Singleton implementation.
class Singleton {
private static $instances = array();

final protected static function _getInstance($className) {
if (!array_key_exists($className, self::$instances)) {
self::$instances[$className] = new $className();
protected function __construct() {
}
return self::$instances[$className];
}

/*
// You should implement this method to the final class. (An example is below.)
// This is mainly because "late static bindings" is supported after PHP 5.3.
final protected static function _getInstance($className) {
if (!array_key_exists($className, self::$instances)) {
self::$instances[$className] = new $className();
}
return self::$instances[$className];
}

public static function getInstance() {
return self::_getInstance(__CLASS__);
/*
// If your model support prior to PHP 5.3, you should implement this method to the final class.
// (An example is below.)
// This is mainly because "late static bindings" is supported after PHP 5.3.
public static function getInstance() {
return self::_getInstance(__CLASS__);
}
*/
public static function getInstance(){}
}
*/
abstract public static function getInstance();
}

/// String manipulation class
Expand Down
4 changes: 2 additions & 2 deletions framework/model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public static function getInstance() {
return self::_getInstance(__CLASS__);
}

protected function __construct($id = 'textcube') {
$this->__basicConfigLoader($id);
public function __construct() {
$this->__basicConfigLoader('textcube');
}

private function __basicConfigLoader($id) {
Expand Down
2 changes: 1 addition & 1 deletion framework/model/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

final class Model_Context extends Singleton
{
private static $__property, $__namespace;
private $__property, $__namespace;
public static function getInstance() {
return self::_getInstance(__CLASS__);
}
Expand Down
4 changes: 0 additions & 4 deletions framework/model/LegacySupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public static function getInstance() {
return self::_getInstance(__CLASS__);
}

protected function __construct() {

}

public function addSupport($parameters) {
if(!is_array($parameters)) {
$parameters = array($parameters);
Expand Down
2 changes: 1 addition & 1 deletion framework/model/URIHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static function getInstance() {
return self::_getInstance(__CLASS__);
}

protected function __construct() {
public function __construct() {
$this->__URIInterpreter();
$this->context = Model_Context::getInstance();
}
Expand Down

0 comments on commit 1367660

Please sign in to comment.