# Inheritance and Static Methods

- Thread: https://externals.io/message/3309
- Original archive: https://news-web.php.net/php.internals/3309
- Started by Phil Dier on 2003-07-08T21:11:49+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3309 — Phil Dier — 2003-07-08T21:11:49+00:00

URL: https://externals.io/message/3309#3309

Is there a function that can be used to retrieve the name of a statically
called child class from within an inherited method? `debug_backtrace()` works
for this purpose, but I've seen people say that it is slow. Consider the
example code below. Is this a planned feature for PHP 5, or could this
possibly make it into 4.x? What options do I have?

<?

class a
{
    function test_backtrace()
    {
        $bt = `debug_backtrace()`;
        echo "class pre: ".__CLASS__."\n";
        echo "class bt: ".$bt[0]["class"]."\n";
        echo "class get_class: ".get_class($this)."\n";
    }
}

class b extends a
{
}

echo "** static: \n";
b::test_backtrace();
$b = new b();
echo "** object: \n";
$b->test_backtrace();

?>

Output Looks Like:

** static: 
class pre: a
class bt: b
class get_class: 
** object: 
class pre: a
class bt: b
class get_class: b


Perhaps `get_class()` could return the expected class name when
called without arguements?


-- 
Phil Dier
gett communications		<http://www.gettcomm.com/>
gettlabs, inc.			<http://www.gettlabs.com/

---

## #3314 — Phil Dier — 2003-07-08T21:41:23+00:00

URL: https://externals.io/message/3309#3314

On Tue, 8 Jul 2003 23:35:14 +0200
marcus.boerger@t-online.de (Marcus Börger) wrote:

[snip]
> - the method is defined in class a so pre is correct
> - backtrace @[0] sees the top caller class that's b that's correct
> - get_class($var) sees the class of the variable. In you static case that is
>   `NULL` which doesn't have a class and in your object case that's b. Therfore
>   this is also correct.
> 
> Everything show is correct so far. The only problem is that test_backtrace()
> could be called static without being defined as such.
> 
[snip]

Thanks for your response.  I wasn't disputing the current behaviour,
only wondering if there is / is planned a function specifically for
doing what I did with `debug_backtrace()` in a static method.  Using
 `debug_backtrace()` seems hackish and slow.

-- 
Phil Dier
gett communications		<http://www.gettcomm.com/>
gettlabs, inc.			<http://www.gettlabs.com/
