note 89478 deleted from function.get-called-class by danbrown

From: Date: Fri, 08 Jun 2012 20:18:57 +0000
Subject: note 89478 deleted from function.get-called-class by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188497@lists.php.net to get a copy of this message
Note Submitter: progman at centrum dot sk ---- Simplified version: <?php if (!function_exists('get_called_class')): function get_called_class() { $bt = debug_backtrace(); $lines = file($bt[1]['file']); preg_match('/([a-zA-Z0-9\_]+)::'.$bt[1]['function'].'/', $lines[$bt[1]['line']-1], $matches); return $matches[1]; } endif; ?> When you use this function in singleton, mind there can by only one getInstance() call per line: <?php abstract class Singleton { ... } class MySingleton1 extends Singleton { ... } class MySingleton2 extends Singleton { ... } // this will work: $s1 = MySingleton1::getInstance(); $s2 = MySingleton2::getInstance(); // here will be problem: $s1 = MySingleton1::getInstance(); $s2 = MySingleton2::getInstance(); ?>

« previous php.notes (#188497) next »