rfc:class_name_scalars

This is an old revision of the document!


Request for Comments: How to write RFCs

Introduction

Reuse the “class” keyword in order to provide class name resolution according to current namespace and use rules.

Proposal

From the test for the feature:

<?php
 
namespace Foo\Bar {
    class Baz {}
    var_dump(Moo::CLASS); // resolve in namespace
    class Box {
        public static function registerClassName($class = Baz::class) {
            var_dump($class);
        }
    }
    Box::registerClassName();
}
 
namespace {
    use Bee\Bop as Moo,
        Foo\Bar\Baz;
    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
    var_dump($x);    
}
 
 
?>
--EXPECTF--    
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) {
}

Patch

Changelog

* 2012-04-17 Initially created by Ralph Schindler

rfc/class_name_scalars.1334673026.txt.gz · Last modified: 2017/09/22 13:28 (external edit)