PHP RFC: Allow ::class on objects


The Foo\Bar::class syntax has been introduced in PHP 5.5 to allow fetching a class name as a string, in a way that respects context-dependent name resolution rules and is understood by IDEs. As it is syntactically similar to a class constant access, programmers intuitively expect the syntax $object::class to work as well and provide the same result as get_class($object). This RFC proposes to allow that syntax.

The original rationale for not allowing the $object::class syntax was that Foo\Bar::class is resolved at compile-time, which is not possible for $object::class. However, this premise is already incorrect for two cases: First, static::class is always resolved at runtime, equivalent to get_called_class(). Second, self::class and parent::class are also sometimes resolved at runtime, for example in closures.

Overall I think that $object::class has a well-defined meaning, and programmers familiar with the Foo\Bar::class syntax generally expect it to be available by symmetry with the general $object::CONST_NAME syntax. Not allowing the syntax is more surprising than allowing it.


$object::class is already permitted on the syntax level, but currently throws a compile error. This error will be removed when used in a normal expression context. However, $object::class will remain forbidden inside a constant expression context (as objects cannot be created there).

If $object is an object, then $object::class returns get_class($object). Otherwise it throws a TypeError exception.

$object = new stdClass;
var_dump($object::class); // "stdClass"
$object = null;
var_dump($object::class); // TypeError

This RFC does not permit $object to be a string and throws a TypeError in that case as well. It would be possible to allow strings to be consistent with the $className::CONST_NAME syntax. In that case, $str::class would return $str verbatim. As this doesn't seem practically useful, and likely somewhat surprising (esp. the fact that the class name is not validated at all, and can contain characters not allowed in class names), this functionality is omitted.

Backward Incompatible Changes

There are no backwards incompatible changes.


Voting started 2020-01-28 and ends 2020-02-11.

Allow ::class on objects?
Real name Yes No
alcaeus (alcaeus)  
asgrim (asgrim)  
ashnazg (ashnazg)  
beberlei (beberlei)  
bishop (bishop)  
brzuchal (brzuchal)  
bwoebi (bwoebi)  
carusogabriel (carusogabriel)  
cmb (cmb)  
colinodell (colinodell)  
derick (derick)  
duncan3dc (duncan3dc)  
ekin (ekin)  
galvao (galvao)  
gasolwu (gasolwu)  
geekcom (geekcom)  
girgias (girgias)  
googleguy (googleguy)  
guilhermeblanco (guilhermeblanco)  
jasny (jasny)  
kalle (kalle)  
kguest (kguest)  
kinncj (kinncj)  
kocsismate (kocsismate)  
kriscraig (kriscraig)  
levim (levim)  
malukenho (malukenho)  
marandall (marandall)  
marcio (marcio)  
mariano (mariano)  
mbeccati (mbeccati)  
mcmic (mcmic)  
mgocobachi (mgocobachi)  
mike (mike)  
narf (narf)  
nicolasgrekas (nicolasgrekas)  
nikic (nikic)  
ocramius (ocramius)  
pierrick (pierrick)  
pmjones (pmjones)  
pollita (pollita)  
ralphschindler (ralphschindler)  
ramsey (ramsey)  
reywob (reywob)  
ruudboon (ruudboon)  
sammyk (sammyk)  
santiagolizardo (santiagolizardo)  
sebastian (sebastian)  
seld (seld)  
sergey (sergey)  
sirsnyder (sirsnyder)  
sorin (sorin)  
stas (stas)  
svpernova09 (svpernova09)  
thekid (thekid)  
trowski (trowski)  
villfa (villfa)  
xatenev (xatenev)  
yunosh (yunosh)  
zimt (zimt)  
Final result: 60 0
This poll has been closed.
rfc/class_name_literal_on_object.txt · Last modified: 2020/02/17 10:51 by nikic