rfc:class_name_scalars
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:class_name_scalars [2012/09/11 14:32] – [Request for Comments: Cution As Scalar Via class Keyword] lass Name Resol ralphschindler | rfc:class_name_scalars [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
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:// | ||
Line 31: | Line 31: | ||
</ | </ | ||
+ | ==== 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 ===== | ===== Choice of syntax ===== | ||
Line 44: | Line 97: | ||
<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 { |
- | var_dump(Moo::class); // resolve in namespace | + | // compile time constants |
- | class Box { | + | const A = self:: |
- | public static function | + | const B = Two:: |
- | var_dump($class); | + | |
+ | | ||
+ | public static function run() { | ||
+ | | ||
+ | var_dump(static:: | ||
+ | var_dump(parent:: | ||
+ | var_dump(Baz:: | ||
+ | } | ||
+ | } | ||
+ | class Three extends Two { | ||
+ | // compile time static lookups | ||
+ | public static function | ||
+ | | ||
+ | $two = Baz::class, | ||
+ | $three = One::A, | ||
+ | $four = self::B | ||
+ | | ||
+ | var_dump($one, $two, $three, $four); | ||
} | } | ||
} | } | ||
- | | + | |
+ | var_dump(Moo::CLASS); // resolve in namespace | ||
} | } | ||
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) " | ||
</ | </ | ||
Line 88: | Line 183: | ||
Do we: | Do we: | ||
- | * Throw a compile error? | + | * <del>Throw a compile error?</ |
- | * Resolve as best as possible, meaning error in non-class context, do a runtime lookup in a class context | + | * <del>Resolve as best as possible, meaning error in non-class context, do a runtime lookup in a class context</ |
+ | |||
+ | **Note:** (as of 1/18/13) | ||
- | Note: (as of 9/8/12) | ||
In the current patch, the following resolutions take place: | In the current patch, the following resolutions take place: | ||
- | * self::class resolves the same as __CLASS__ would | + | * self::class resolves the same as %%__CLASS__%% would |
* static:: | * static:: | ||
* parent:: | * parent:: | ||
+ | * static:: | ||
Line 105: | Line 202: | ||
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * 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-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) | * 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.1347373954.txt.gz · Last modified: 2017/09/22 13:28 (external edit)