This is an old revision of the document!

Request for Comments: Class Name Resolution As Scalar Via "class" Keyword


This RFC proposes to add a new ClassName::class syntax, which provides the fully qualified class name as a string.

Use case

Class names in strings have to be fully qualified in PHP. It is not possible to utilize aliases registered through use statements:

use A\Namespaced\ClassName;
// this will try to create a mock of the global class ClassName, not
// of the aliased class A\Namespaced\ClassName
$mock = $this->getMock('ClassName');

ClassName::class allows the programmer to easily obtain the fully qualified class name from an aliased name:

use A\Namespaced\ClassName;
// ClassName::class resolves to 'A\Namespaced\ClassName'
$mock = $this->getMock(ClassName::class);

Choice of syntax

The ClassName::class syntax was chosen because it can not clash with existing constants (as class is a keyword). The feature addition thus is fully backwards compatible.

Furthermore the syntax resembles the semantically equivalent Java syntax ClassName.class.

Note: since this feature reuses T_CLASS/class keyword, it is case-insensitive. Therefore, Foo::class is semantically equivalent to Foo::Class, Foo::CLASS and the like.

More examples

From the test for the feature:

namespace Foo\Bar {
    class Baz {}
    var_dump(Moo::class); // resolve in namespace
    class Box {
        public static function registerClassName($class = Baz::class) {
namespace {
    use Bee\Bop as Moo,
    var_dump(Baz::class); // resolve from use
    var_dump(Boo::class); // resolve in global namespace
    var_dump(Moo::class); // resolve from use as
    var_dump(\Moo::class); // resolve fully qualified
    $class = Baz::class; // assign class as scalar to var
    $x = new $class; // create new class from original scalar assignment
string(11) "Foo\Bar\Moo"
string(11) "Foo\Bar\Baz"
string(11) "Foo\Bar\Baz"
string(3) "Boo"
string(7) "Bee\Bop"
string(3) "Moo"
object(Foo\Bar\Baz)#1 (0) {


One situation in need of a solution is what to do with self::class, static::class, and parent::class

Do we:

  • Throw a compile error?
  • Resolve as best as possible, meaning error in non-class context, do a runtime lookup in a class context

Note: (as of 9/8/12) In the current patch, the following resolutions take place:

  • self::class resolves the same as CLASS would
  • static::class resolves the same as get_called_class() would
  • parent::class resolves the same as get_parent_class() would, (in fact, would return false if not inherited.)


* Pull Request located here: https://github.com/php/php-src/pull/187/files


  • 2012-04-17 Initially created by Ralph Schindler
  • 2012-09-08 Updated RFC and added link to Pull Request (which addresses considerations and on-list questions)
rfc/class_name_scalars.1347144013.txt.gz · Last modified: 2017/09/22 13:28 (external edit)