Bug #65419 [Com]: Inside trait, self::class != __CLASS__

From: Date: Wed, 14 Aug 2013 10:10:48 +0000
Subject: Bug #65419 [Com]: Inside trait, self::class != __CLASS__
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-180912@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65419&edit=1

 ID:                 65419
 Comment by:         nicolas dot grekas+php at gmail dot com
 Reported by:        nicolas dot grekas+php at gmail dot com
 Summary:            Inside trait, self::class != __CLASS__
 Status:             Verified
 Type:               Bug
 Package:            Scripting Engine problem
 PHP Version:        5.5.1
 Block user comment: N
 Private report:     N

 New Comment:

Why remove ::class support inside traits? It mostly works today, and it is as usefull there than
inside classes or anywhere else, am I wrong?

Would it be possible to replace self::class occurences by __CLASS__ at compile time so that the code
path for __CLASS__ is also used here?


Previous Comments:
------------------------------------------------------------------------
[2013-08-11 15:31:06] laruence@php.net

I prefer to trigger a COMPILE ERROR prevent from using ::class in a traits

------------------------------------------------------------------------
[2013-08-09 07:57:41] gron@php.net

Yes, that looks wrong. It should indeed behave as __CLASS__.

------------------------------------------------------------------------
[2013-08-09 07:55:23] nicolas dot grekas+php at gmail dot com

I totally agree with you.

This reminds me the discussion that happened in https://bugs.php.net/55214 and that led to this special
handling for __CLASS__.

I think that the very same arguments apply here also.

Concerning parent and static, they are free from this problem because they are resolved at run-time
(tested, it works).

------------------------------------------------------------------------
[2013-08-08 18:33:55] requinix@php.net

The RFC was created a few weeks after 5.4.0 was released and does not specify how self::class would
work for traits. I would take that to mean the writer did not consider how it would 
work for traits, rather than that traits should "inherit" the behavior for classes.

So either this is a doc bug and the ::class doc should state that it resolves to the trait itself
when used for traits (as a "::trait" syntax would be weird), or self::class is fixed so 
be identical to __CLASS__. Presumably parent::class and static::class would receive similar changes.

Note for the latter: in zend_language_scanner, __CLASS__ has special handling when used inside
traits. I imagine similar logic - creating "a special __CLASS__ constant" - would be used
in 
zend_do_resolve_class_name().

------------------------------------------------------------------------
[2013-08-08 08:44:05] nicolas dot grekas+php at gmail dot com

Description:
------------
The RFC for ::class name resolution as scalar say that self::class resolves the same as __CLASS__:

https://wiki.php.net/rfc/class_name_scalars#considerations

But this is not true when using traits.

Test script:
---------------
<?php

trait abc
{
  static function def()
  {
    echo self::class, "\n";
    echo __CLASS__, "\n";
}

class ghi
{
  use abc;
}

ghi::def();

Expected result:
----------------
ghi
ghi

Actual result:
--------------
abc
ghi


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65419&edit=1


Thread (12 messages)

« previous php.bugs (#180912) next »