How to get the name of the calling class (in PHP) How to get the name of the calling class (in PHP) php php

How to get the name of the calling class (in PHP)


If anyone came here looking for how to get the name of a calling class from another class like I did, check this out https://gist.github.com/1122679

EDIT: pasted code

function get_calling_class() {    //get the trace    $trace = debug_backtrace();    // Get the class that is asking for who awoke it    $class = $trace[1]['class'];    // +1 to i cos we have to account for calling this function    for ( $i=1; $i<count( $trace ); $i++ ) {        if ( isset( $trace[$i] ) ) // is it set?             if ( $class != $trace[$i]['class'] ) // is it a different class                 return $trace[$i]['class'];    }}

EG

class A {    function t() {        echo get_calling_class();    }}class B {    function x() {        $a = new A;        $a->t();    }}$b = new B;$b->x(); // prints B


Use get_class():

$this->callbacks[$onAction][] = $callbackMethod;$className = get_class($this);// Call callback method$className->$callbackMethod();


You should really do something like:

$this->registerCallback(array($this, 'onTiny'), anActionType);

That is how PHP works with handles to object methods.