Doc #80764 [Com]: method magic constant in closure loses class
| From: | garygarza197 at gmail dot com | Date: | Mon, 22 Feb 2021 06:45:58 +0000 |
| Subject: | Doc #80764 [Com]: method magic constant in closure loses class | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-18580@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80764&edit=1
ID: 80764
Comment by: garygarza197 at gmail dot com
Reported by: bernievbb at icloud dot com
Summary: method magic constant in closure loses class
Status: Verified
Type: Documentation Problem
Package: *General Issues
Operating System: macos
PHP Version: 8.0Git-2021-02-17 (Git)
Block user comment: N
Private report: N
New Comment:
1
This is the solution I ended up using. It's not very good but probably better than using the
debug_backtrace function.
Problem example:
Trait ExampleTrait {
protected function doSomethingRecursive() {
// this is a problem because it could be renamed
$this->doSomethingRecursive();
}
}
Solution example:
Trait ExampleTrait {
protected function doSomethingRecursive() {
// this is a problem because it could be renamed
$this->__internal_doSomethingRecursive();
}
private function __internal_doSomethingRecursive() {
// this works because the class would have
// used and renamed the above function
// but this "internal" function *should*
// remain available under it's original name
$this->__internal_doSomethingRecursive();
}
}
Of course it's possible to break this but for most cases it should be fine. You could also
include the name of the trait in the internal function name to further prevent conflicts https://www.myaarpmedicare.life/
Previous Comments:
------------------------------------------------------------------------
[2021-02-18 15:12:16] 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-02-18 11:18:34] cmb@php.net
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] <https://3v4l.org/fr6E6>
[2] <https://www.php.net/manual/en/language.constants.magic.php>
------------------------------------------------------------------------
[2021-02-17 18:15:04] bernievbb at icloud dot com
Description:
------------
class and method are lost when __METHOD__ called in closure.
Test script:
---------------
<?php
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}"
}
}?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80764&edit=1