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

39,572

Solution 1

Use get_class():

$this->callbacks[$onAction][] = $callbackMethod;
$className = get_class($this);

// Call callback method
$className->$callbackMethod();

Solution 2

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

Solution 3

You should really do something like:

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

That is how PHP works with handles to object methods.

Solution 4

From PHP 8+ you can use static::class rather than get_class($this).

This one is also auto-fixed with PHP Code Sniffer and rule SlevomatCodingStandard.Classes.ModernClassNameReference

Share:
39,572

Related videos on Youtube

Mark Tomlin
Author by

Mark Tomlin

Started Using Computers in 1994. Started Programming Computers in 2004. Started Professionally Programming Computers in 2006. Strong Background in HTML, PHP &amp; CSS. Intermediate Background in JavaScript, TypeScript, &amp; Rust. Weak background in C, C++ &amp; PAWN (SmallC).

Updated on July 09, 2022

Comments

  • Mark Tomlin
    Mark Tomlin almost 2 years
    define('anActionType', 1);
    $actionTypes = array(anActionType => 'anActionType');
    class core {
        public $callbacks = array();
        public $plugins = array();
        public function __construct() {
            $this->plugins[] = new admin();
            $this->plugins[] = new client();
        }
    }
    abstract class plugin {
        public function registerCallback($callbackMethod, $onAction) {
            if (!isset($this->callbacks[$onAction]))
                $this->callbacks[$onAction] = array();
    
            global $actionTypes;
            echo "Calling $callbackMethod in $callbacksClass because we got {$actionTypes[$onAction]}" . PHP_EOL;
    
            // How do I get $callbacksClass?
    
            $this->callbacks[$onAction][] = $callbackMethod;
        }
    }
    class admin extends plugin {
        public function __construct() {
            $this->registerCallback('onTiny', anActionType);
        }
        public function onTiny() { echo 'tinyAdmin'; }
    }
    class client extends plugin {
        public function __construct() {
            $this->registerCallback('onTiny', anActionType);
        }
        public function onTiny() { echo 'tinyClient'; }
    }
    $o = new core();
    

    $callbacksClass should be admin or client. Or am I missing the point here completely and should go about this another way? It should be noted that I will only accept an answer that does not require me to send the classname as an argument to the registerCallback method.

  • Theodore R. Smith
    Theodore R. Smith over 13 years
    That's how the PHP internal callback functions (like preg_replace_callback) work, but not his class. His class would break if he used that syntax.
  • Matthew
    Matthew over 13 years
    He should change his class to work with normal PHP callbacks, unless there's a very good reason not to.
  • Mark Tomlin
    Mark Tomlin over 13 years
    It's exactly what I was looking for. :)
  • user23127
    user23127 about 10 years
    This does not return the calling class, just the class of $this. If you call registerCallback with another class it would fail. Either make registerCallback protected or at use matthews approach.
  • Arxeiss
    Arxeiss over 2 years
    You can also use static::class instead of get_class($this). Works only in PHP 8+ if I'm not wrong
  • Mark Tomlin
    Mark Tomlin over 2 years
    Ah, nice thanks for the update on this!