note 89478 added to function.get-called-class

From: Date: Tue, 10 Mar 2009 08:49:54 +0000
Subject: note 89478 added to function.get-called-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-151677@lists.php.net to get a copy of this message
A small correction on my last post. $matches[0] should be $matches[1]. 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(); ?> ---- Server IP: 217.73.17.12 Probable Submitter: 213.160.186.25 ---- Manual Page -- http://www.php.net/manual/en/function.get-called-class.php Edit -- https://master.php.net/note/edit/89478 Del: integrated -- https://master.php.net/note/delete/89478/integrated Del: useless -- https://master.php.net/note/delete/89478/useless Del: bad code -- https://master.php.net/note/delete/89478/bad+code Del: spam -- https://master.php.net/note/delete/89478/spam Del: non-english -- https://master.php.net/note/delete/89478/non-english Del: in docs -- https://master.php.net/note/delete/89478/in+docs Del: other reasons-- https://master.php.net/note/delete/89478 Reject -- https://master.php.net/note/reject/89478 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151677) next »