|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80764 __METHOD__ in closure looses class
Submitted: 2021-02-17 18:15 UTC Modified: 2021-08-26 12:08 UTC
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: bernievbb at icloud dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 8.0Git-2021-02-17 (Git) OS: macos
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
49 - 4 = ?
Subscribe to this entry?

 [2021-02-17 18:15 UTC] bernievbb at icloud dot com
class and method are lost when __METHOD__ called in closure.

Test script:
 namespace pdox\pal;
class test
    function test1()
        var_dump(__METHOD__); // string(21) "pdox\pal\test::test1"

        $a = function() {return __METHOD__;};
        var_dump($a());                // string(18) "pdox\pal\{closure}"

        $b = fn() => __METHOD__;
        var_dump($b());               // string(18) "pdox\pal\{closure}"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-18 11:18 UTC]
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2021-02-18 11:18 UTC]
For funtions, __METHOD__ is a synonym for __FUNCTION__[1].
Anonymous functions are *functions*, so this behaves as intended,
although the documentation[2] should be improved.

[1] <>
[2] <>
 [2021-02-18 15:12 UTC] bernievbb at icloud dot com
Clearly __METHOD__ is NOT just a synonym for __FUNCTION__ ...

All inside a namespaced class method:

NOT in closure:

__FUNCTION__ => method name
__METHOD__   => namespace + className + "::" + methodName
__CLASS__    => namespace + className

IN a closure:

__FUNCTION__ => namespace + "{closure}"
__METHOD__   => namespace + "{closure}"
__CLASS__    => namespace + className

I would expect __METHOD__ to report the closure's parent method.

To be consistent with not in a closure I would also reasonably expect __METHOD__ to provide the class name.

__METHOD__ and __FUNCTION__ are useless in a closure. Surely this is not intended.

I see no simple alternative. Think debugging, logging etc.
 [2021-08-26 12:08 UTC]
-Summary: method magic constant in closure loses class +Summary: __METHOD__ in closure looses class -Type: Documentation Problem +Type: Bug -Package: *General Issues +Package: Scripting Engine problem
 [2021-08-26 12:08 UTC]
Oh, I was too fast here, and missed your reply.  This appears to
be an actual bug.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 17:01:33 2024 UTC