Bug #72916 [Com]: get_called_class is wrong after calling via self:: from non-static method

From: Date: Wed, 12 Apr 2017 19:37:48 +0000
Subject: Bug #72916 [Com]: get_called_class is wrong after calling via self:: from non-static method
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-208497@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72916&edit=1

 ID:                 72916
 Comment by:         mail at pmmaga dot net
 Reported by:        gharlan at web dot de
 Summary:            get_called_class is wrong after calling via self::
                     from non-static method
 Status:             Open
 Type:               Bug
 Package:            Class/Object related
 PHP Version:        7.0.10
 Block user comment: N
 Private report:     N

 New Comment:

Is it possible you are expecting the behavior of get_class instead of get_called_class?

From the documentation, get_called_class: "Gets the name of the class the static method is
called in."

In your example, the result "Child" follows that rule.


Previous Comments:
------------------------------------------------------------------------
[2016-08-21 23:18:21] gharlan at web dot de

Description:
------------
The return value of get_called_class() is wrong, when it is called from a static method that is
called via self::method() in a non-static method.

Test script:
---------------
<?php
class Foo {
    protected static function getClass() {
        return get_called_class();
    }
    
    public function bar() {
        echo 'Foo: ', Foo::getClass(), "\n";
        echo 'self: ', self::getClass(), "\n";
        echo 'static: ', static::getClass(), "\n";
    }
}

class Child extends Foo {
    protected static function getClass() {
        return 'x';
    }
}

(new Child)->bar();

Expected result:
----------------
Foo: Foo
self: Foo
static: x

Actual result:
--------------
Foo: Foo
self: Child
static: x


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=72916&edit=1


Thread (3 messages)

« previous php.bugs (#208497) next »