0

I have a series of inherited classes using a lot of method overloading and I need a way to find out which class is being used when it calls a particular method.

simple example:

class child extends parent
    public function _process()
    {
        $this->_otherrun();
    }

    public function _run()    

class parent extends grandparent
    public function _run()
    public function _otherrun()
    {
        // I need to find out which class the version of _run is called in
        $this->_run();
    }

this example is quite simple compared to what I'm looking through, so I need a way to see in which class function _run is processed in. Is that even possible?

Something like this:

get_class(array($this, 'run'));
Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
  • Isn't this just a modified version of this: http://stackoverflow.com/questions/10493720/how-to-get-the-protected-stastic-value-from-subclss-in-the-main-scope/10494032#10494032 Obviously you wouldn't need the echo in the constuct, and the "get_table_name_protected" would change to "_run". But yeah... looks about like what your going for, unless I messed something. – Dave Holitish May 07 '14 at 22:48

1 Answers1

0

You can use get_called_class() function or debug_backtrace() function:

get_called_class() example:

class foo {
    static public function test() {
        var_dump(get_called_class());
    }
}

debug_backtrace() example (code part):

<?php
// filename: /tmp/a.php

function a_test($str)
{
    echo "\nHi: $str";
    var_dump(debug_backtrace());
}

a_test('friend');
?>

<?php
// filename: /tmp/b.php
include_once '/tmp/a.php';
?>

Results of code above:

Hi: friend
array(2) {
[0]=>
array(4) {
    ["file"] => string(10) "/tmp/a.php"
    ["line"] => int(10)
    ["function"] => string(6) "a_test"
    ["args"]=>
    array(1) {
      [0] => &string(6) "friend"
    }
}
[1]=>
array(4) {
    ["file"] => string(10) "/tmp/b.php"
    ["line"] => int(2)
    ["args"] =>
    array(1) {
      [0] => string(10) "/tmp/a.php"
    }
    ["function"] => string(12) "include_once"
  }
}

Also, use search. One of results: Find out which class called a method in another class

Updated (comments): use debug_backtrace() function.

In your case:

class parent extends grandparent
public function _run()
public function _otherrun()
{
    // I need to find out which class the version of _run is called in
    $this->_run();
    var_dump(debug_backtrace());
}
Community
  • 1
  • 1
Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
  • only problem is, I don't know which class the function is being called in, otherwise I would know the class already. – Francis Lewis May 07 '14 at 22:47
  • @FrancisLewis updated it again. Examples are from sources. If you need you can research better. Going to sleep now! Bye :) Good luck with your work. – Sharikov Vladislav May 07 '14 at 22:54