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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

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: Thu Apr 25 22:01:29 2024 UTC