Re: Re: [RFC] Access to aliases definition by reflection
| From: | Miloslav Hůla | Date: | Fri, 31 Oct 2014 12:48:33 +0000 |
| Subject: | Re: Re: [RFC] Access to aliases definition by reflection | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-78506@lists.php.net to get a copy of this message | ||
Dne 31.10.2014 12:00, Chris Wright napsal(a):
I agree with Levi, this doesn't make a lot of sense. The only thing that would make sense in terms of supporting aliases for me would be this: <?php use Library\Http\Clients\CurlClient as HttpClient; $rc = new \ReflectionClass('HttpClient'); var_dump($rc->getName()); // string(31) "Library\Http\Clients\CurlClient"Alias support is not needed here, HttpClient::CLASS can be used for expansion.
I cannot see the value in being able to inspect the compile time constructs at run time, but I can see the value in being able to *use* them in strings at run time.It starts to make sense, when you use kind of "code analysis". For following techniques, getDefinedAliases() has an added value: <?php use Library\Http\Clients\CurlClient as HttpClient; # Dependency Injection (public property injection) class Downloader { /** * @var HttpClient * @inject */ public $httpClient; } # Factory class HttpClientFactory { /** @return HttpClient */ public function create() { } } This code is analysed in other file. For example in some dependency injection container. In this container, you must care what string 'HttpClient' in annotations really means. What it means in context of classes Downloader and HttpClientFactory. I agree with Levi, that using FQN in annotations works. And I wrote reasons why it is wrong. Moreover, IDEs works with aliases in annotations well. The only missing fragment is, that you cannot access aliases definitions in run-time. That's the reason for the RFC.
The getDefinedAliases() code depicted above doesn't really belong on a ReflectionClass, or any of the other classes that currently exist, as the aliases aren't associated with the class/function itself but the environment in which it was defined.An alias can be defined (almost) anywhere. Following is valid PHP code: <?php use Library\One; class C {} use Library\Two; class D {} Alias 'One' exists for class C and D. But alias 'Two' exists only for class D. If you want to resolve an alias to FQN, you must choose a context. But where to get the context in run-time? Class or function reflection seems right. Thank you, Milo