note 106368 modified in function.get-class by danbrown

From: Date: Fri, 04 Nov 2011 14:00:55 +0000
Subject: note 106368 modified in function.get-class by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184302@lists.php.net to get a copy of this message
Attempting various singleton base class methods described on this page, I have created a base class and bridge function that allows it to work without get_called_class() if it's not available. Unlike other methods listed here, I chose not to prevent use of __construct() or __clone(). <?php abstract class Singleton { protected static $m_pInstance; final public static function getInstance(){ $class = static::getClass(); if(!isset(static::$m_pInstance[$class])) { static::$m_pInstance[$class] = new $class; } return static::$m_pInstance[$class]; } final public static function getClass(){ return get_called_class(); } } // I don't remember where I found this, but this is to allow php < 5.3 to use this method. if (!function_exists('get_called_class')) { function get_called_class($bt = false, $l = 1) { if (!$bt) $bt = debug_backtrace(); if (!isset($bt[$l])) throw new Exception("Cannot find called class -> stack level too deep."); if (!isset($bt[$l]['type'])) { throw new Exception('type not set'); } else switch ($bt[$l]['type']) { case '::': $lines = file($bt[$l]['file']); $i = 0; $callerLine = ''; do { $i++; $callerLine = $lines[$bt[$l]['line'] - $i] . $callerLine; } while (stripos($callerLine, $bt[$l]['function']) === false); preg_match('/([a-zA-Z0-9\_]+)::' . $bt[$l]['function'] . '/', $callerLine, $matches); if (!isset($matches[1])) { // must be an edge case. throw new Exception("Could not find caller class: originating method call is obscured."); } switch ($matches[1]) { case 'self': case 'parent': return get_called_class($bt, $l + 1); default: return $matches[1]; } // won't get here. case '->': switch ($bt[$l]['function']) { case '__get': // edge case -> get class of calling object if (!is_object($bt[$l]['object'])) throw new Exception("Edge case fail. __get called on non object."); return get_class($bt[$l]['object']); default: return $bt[$l]['class']; } default: throw new Exception("Unknown backtrace method type"); } } } class B extends Singleton { } class C extends Singleton { } $b = B::getInstance(); echo 'class: '.get_class($b); echo '<br />'; $c = C::getInstance(); echo echo 'class: '.get_class($c); ?> This returns: class: b class: c --was-- Attempting various singleton base class methods described on this page, I have created a base class and bridge function that allows it to work without get_called_class() if it's not available. Unlike other methods listed here, I chose not to prevent use of __construct() or __clone(). <?php abstract class Singleton { protected static $m_pInstance; final public static function getInstance(){ $class = static::getClass(); if(!static::$m_pInstance[$class]) { static::$m_pInstance[$class] = new $class; } return static::$m_pInstance[$class]; } final public static function getClass(){ return get_called_class(); } } // I don't remember where I found this, but this is to allow php < 5.3 to use this method. if (!function_exists('get_called_class')) { function get_called_class($bt = false, $l = 1) { if (!$bt) $bt = debug_backtrace(); if (!isset($bt[$l])) throw new Exception("Cannot find called class -> stack level too deep."); if (!isset($bt[$l]['type'])) { throw new Exception('type not set'); } else switch ($bt[$l]['type']) { case '::': $lines = file($bt[$l]['file']); $i = 0; $callerLine = ''; do { $i++; $callerLine = $lines[$bt[$l]['line'] - $i] . $callerLine; } while (stripos($callerLine, $bt[$l]['function']) === false); preg_match('/([a-zA-Z0-9\_]+)::' . $bt[$l]['function'] . '/', $callerLine, $matches); if (!isset($matches[1])) { // must be an edge case. throw new Exception("Could not find caller class: originating method call is obscured."); } switch ($matches[1]) { case 'self': case 'parent': return get_called_class($bt, $l + 1); default: return $matches[1]; } // won't get here. case '->': switch ($bt[$l]['function']) { case '__get': // edge case -> get class of calling object if (!is_object($bt[$l]['object'])) throw new Exception("Edge case fail. __get called on non object."); return get_class($bt[$l]['object']); default: return $bt[$l]['class']; } default: throw new Exception("Unknown backtrace method type"); } } } class B extends Singleton { } class C extends Singleton { } $b = B::getInstance(); echo 'class: '.get_class($b); echo '<br />'; $c = C::getInstance(); echo echo 'class: '.get_class($c); ?> This returns: class: b class: c http://php.net/manual/en/function.get-class.php

« previous php.notes (#184302) next »