php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67577 Unintuitive __CLASS__ magic const behaviour when in a closure from a trait.
Submitted: 2014-07-06 02:59 UTC Modified: 2021-11-17 17:27 UTC
From: elliot at aanet dot com dot au Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 5.6.0RC2 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-07-06 02:59 UTC] elliot at aanet dot com dot au
Description:
------------
The __CLASS__ magic constant behaves very differently for closures defined in a trait.
It seems to use the bound scope class for the closure rather than the class which it was actually used in.

You can see the current behaviour here:
http://3v4l.org/WIK1E#v540

This can get even more unintuitive when using normal methods and reflection to get them as a closure seen here: 
http://3v4l.org/EhVlW#v540

Considering traits are are essentially copy and pasted into the used class, would it not be more correct to use the class in which it was used in instead?

Expected result:
----------------
Before bind: 
__CLASS__ in class: 
string(17) "ClassConstantTest"
__CLASS__ in Trait: 
string(17) "ClassConstantTest"

After bind: 
__CLASS__ in class: 
string(17) "ClassConstantTest"
__CLASS__ in Trait: 
string(17) "ClassConstantTest"

Actual result:
--------------
Before bind: 
__CLASS__ in class: 
string(17) "ClassConstantTest"
__CLASS__ in Trait: 
string(17) "ClassConstantTest"

After bind: 
__CLASS__ in class: 
string(17) "ClassConstantTest"
__CLASS__ in Trait: 
string(8) "stdClass"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-17 17:27 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-11-17 17:27 UTC] cmb@php.net
Yes, that doesn't look right: <https://3v4l.org/qn3nQ>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 00:01:30 2024 UTC