rfc:class_name_scalars
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:class_name_scalars [2012/04/17 15:16] – ralphschindler | rfc:class_name_scalars [2013/01/19 01:50] – nikic | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2012-04-17 | * Date: 2012-04-17 | ||
* Author: Ralph Schindler < | * Author: Ralph Schindler < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Reuse the " | + | This RFC proposes to add a new '' |
- | ===== Proposal | + | ===== Use case ===== |
+ | |||
+ | Class names in strings have to be fully qualified in PHP. It is not possible to utilize aliases registered through '' | ||
+ | |||
+ | <code php> | ||
+ | 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-> | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | <code php> | ||
+ | use A\Namespaced\ClassName; | ||
+ | |||
+ | // ClassName:: | ||
+ | $mock = $this-> | ||
+ | </ | ||
+ | |||
+ | ==== Real World Usage Scenarios ==== | ||
+ | |||
+ | 1. PHPUnit' | ||
+ | |||
+ | <code php> | ||
+ | |||
+ | use MyVendor\SomeComponent\TargetSubNs; | ||
+ | |||
+ | // inside a test case | ||
+ | $this-> | ||
+ | // as opposed to | ||
+ | $this-> | ||
+ | </ | ||
+ | |||
+ | 2. Doctrine | ||
+ | |||
+ | <code php> | ||
+ | |||
+ | use MyVendor\SomeComponent\TargetEntityNs as Entity; | ||
+ | |||
+ | // inside a test case | ||
+ | $entityManager-> | ||
+ | // as opposed to | ||
+ | $entityManager-> | ||
+ | |||
+ | </ | ||
+ | |||
+ | 3. Any Real (auto-wiring || auto-instantiating) Dependency Injection Container | ||
+ | |||
+ | <code php> | ||
+ | |||
+ | use MyVendor\SomeComponent\TargetNs as T; | ||
+ | |||
+ | // inside a test case | ||
+ | $dic-> | ||
+ | // as opposed to | ||
+ | $dic-> | ||
+ | |||
+ | </ | ||
+ | |||
+ | 4. General PHP Usage: | ||
+ | |||
+ | <code php> | ||
+ | |||
+ | use MyVendor\SomeComponent\TargetNs as T; | ||
+ | |||
+ | // reflection | ||
+ | $r = new ReflectionClass(T\Foo:: | ||
+ | |||
+ | // class_exists, | ||
+ | if (class_exists(T\Foo:: | ||
+ | |||
+ | </ | ||
+ | ===== Choice of syntax ===== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | Furthermore the syntax resembles the semantically equivalent Java syntax '' | ||
+ | |||
+ | Note: since this feature reuses T_CLASS/ | ||
+ | |||
+ | ===== More examples | ||
From the test for the feature: | From the test for the feature: | ||
<code php> | <code php> | ||
+ | |||
+ | --TEST-- | ||
+ | class name as scalar from ::class keyword | ||
+ | --FILE-- | ||
<?php | <?php | ||
namespace Foo\Bar { | namespace Foo\Bar { | ||
- | class Baz {} | + | class One { |
- | | + | // compile time constants |
- | class Box { | + | const A = self::class; |
- | public static function | + | const B = Two::class; |
- | var_dump($class); | + | } |
+ | class Two extends One { | ||
+ | public static function | ||
+ | var_dump(self:: | ||
+ | var_dump(static::class); // runtime lookup | ||
+ | var_dump(parent::class); | ||
+ | var_dump(Baz:: | ||
} | } | ||
} | } | ||
- | | + | |
+ | // compile time static lookups | ||
+ | public static function checkCompileTime( | ||
+ | $one = self::class, | ||
+ | $two = Baz:: | ||
+ | $three = One::A, | ||
+ | $four = self::B | ||
+ | ) { | ||
+ | var_dump($one, $two, $three, $four); | ||
+ | } | ||
+ | } | ||
+ | echo "In NS\n"; | ||
+ | var_dump(Moo:: | ||
} | } | ||
namespace { | namespace { | ||
use Bee\Bop as Moo, | use Bee\Bop as Moo, | ||
- | Foo\Bar\Baz; | + | Foo\Bar\One; |
- | var_dump(Baz::class); // resolve from use | + | echo " |
+ | var_dump(One::class); // resolve from use | ||
var_dump(Boo:: | var_dump(Boo:: | ||
var_dump(Moo:: | var_dump(Moo:: | ||
var_dump(\Moo:: | var_dump(\Moo:: | ||
- | $class = Baz::class; // assign class as scalar to var | + | $class = One::class; // assign class as scalar to var |
$x = new $class; // create new class from original scalar assignment | $x = new $class; // create new class from original scalar assignment | ||
- | var_dump($x); | + | var_dump($x); |
+ | Foo\Bar\Two:: | ||
+ | echo " | ||
+ | Foo\Bar\Three:: | ||
+ | echo " | ||
+ | Foo\Bar\Three:: | ||
} | } | ||
- | |||
?> | ?> | ||
- | --EXPECTF-- | + | --EXPECTF-- |
+ | In NS | ||
string(11) " | string(11) " | ||
- | string(11) " | + | Top |
- | string(11) " | + | string(11) " |
string(3) " | string(3) " | ||
string(7) " | string(7) " | ||
string(3) " | string(3) " | ||
- | object(Foo\Bar\Baz)#1 (0) { | + | object(Foo\Bar\One)#1 (0) { |
} | } | ||
+ | string(11) " | ||
+ | string(11) " | ||
+ | string(11) " | ||
+ | string(11) " | ||
+ | Parent | ||
+ | string(11) " | ||
+ | string(13) " | ||
+ | string(11) " | ||
+ | string(11) " | ||
+ | Compile Check | ||
+ | string(13) " | ||
+ | string(11) " | ||
+ | string(11) " | ||
+ | string(11) " | ||
</ | </ | ||
+ | |||
+ | ===== Considerations ===== | ||
+ | |||
+ | One situation in need of a solution is what to do with '' | ||
+ | |||
+ | Do we: | ||
+ | |||
+ | * < | ||
+ | * < | ||
+ | |||
+ | **Note:** (as of 1/18/13) | ||
+ | |||
+ | In the current patch, the following resolutions take place: | ||
+ | * self::class resolves the same as %%__CLASS__%% would | ||
+ | * static:: | ||
+ | * parent:: | ||
+ | * static:: | ||
+ | |||
===== Patch ===== | ===== Patch ===== | ||
- | * Patch located here: https:// | + | * Pull Request |
+ | |||
+ | ===== Vote ===== | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | Note: This Vote has been closed as the RFC will be moved back to discussion at the time being while the RFC and associated patch become more mature. | ||
===== Changelog ===== | ===== Changelog ===== | ||
- | * 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) | ||
+ | * 2013-01-18 Updated RFC to reflect design decisions, updated the test/ | ||
rfc/class_name_scalars.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1