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 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:
MUST BE VALID
Solve the problem:
19 - 4 = ?
Subscribe to this entry?

 
 [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 19 11:01:28 2024 UTC