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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: elliot at aanet dot com dot au
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Sat Oct 12 12:01:27 2024 UTC