Re: Late FQCN resolution using ::class
| From: | Jens Riisom Schultz | Date: | Thu, 28 Feb 2013 07:03:12 +0000 |
| Subject: | Re: Late FQCN resolution using ::class | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-66314@lists.php.net to get a copy of this message | ||
On Feb 26, 2013, at 10:00 AM, Benjamin Eberlei <kontakt@beberlei.de> wrote:
> This is indeed not possible, because strings are not class context independent, you can pass
> them to anywhere. A string just doesn't know what namespace it belongs to and this does not
> make sense without providing more context in client libraries (such as docblocks).
>
> Also the use statement information is NOT available in the engine, because its resolved way
> before in the compiling step (as far as i understood it).
Thankyou for clarifying this. It was really all I wanted to know.
I guess this discussion is dead (though it birthed a different discussion, which is also impossible
to follow since it is laced with flames)
I'm pulling out of this one. Thankyou all.
>
> What Doctrine Annotations does to solve this is parsing this information ourselves, using
> token_get_all(), then caching this information on a per file basis.
>
>
> On Tue, Feb 26, 2013 at 8:25 AM, Jens Riisom Schultz <ibmurai@me.com> wrote:
> Ok I get that, thankyou for the explanation.
>
> static::class is not an option. I'm trying to resolve class names defined in docblocks,
> since phpdoc2 allows for entering type hints (classes) as namespace/use relative. And I can tell
> there is no current way of resolving class names in strings, to FQCN's, unless I'm missing
> something? (There is no way to get a list of the currently used namespaces as far as I can tell -
> would such a function be possible to add to the SPL, without any major rewriting?)
>
> So, I'm simply wondering if this would require any major rewriting to support? Otherwise I
> could look into it and try to write a patch... Because I think this would be really useful for
> framework developers, php unit testing and php doc for example.
>
> -Jens
>
> On Feb 25, 2013, at 11:20 AM, Nikita Nefedov <inefedor@gmail.com> wrote:
>
> > On Mon, 25 Feb 2013 14:00:04 +0400, Jens Riisom Schultz <ibmurai@me.com> wrote:
> >
> >> Hi everybody,
> >>
> >> I have read up on this, and done some testing.
> >>
> >> First up, my findings with PHP5.5 alpha5:
> >>
> >> <?php
> >> namespace spacy;
> >>
> >> class classy {
> >> public static function fqcn() {
> >> /* This works but is not useful enough: */
> >> //return self::class;
> >>
> >> $me = 'classy';
> >>
> >> /* This just doesn't work, but I wish it did: */
> >> //return $me::class;
> >>
> >> /* This simply does not work as expected: */
> >> return eval("return $me::class;");
> >> /* Output: "classy" - Expected output:
> >> "spacy\classy" */
> >> }
> >> }
> >> ?>
> >>
> >> I'm trying to late resolve a class name contained in a variable to the FQCN. I
> >> understand that this is hard (maybe even impossible) with the current implementation, because class
> >> name resolution happens compile time, but eval("return $me::class;") simply returns
> >> something that is weird.
> >>
> >> I guess what I'm trying to ask is whether it would be impossible to support late
> >> FQCN resolution in any way? It would be very useful for frameworks to be able to do this.
> >>
> >> - Jens Riisom Schultz
> >> --
> >> PHP Internals - PHP Runtime Development Mailing List
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >>
> >
> > Hi Jens,
> >
> > Here's what happened in your code:
> > When you invoked fqcn(), you created var $me = "classy";
> > Then you tried to invoke this code in eval: "return classy::class;"
> > But when php evals code, it's like including another file. So it executes the code
> > without any namespace (it's in global namespace), and php didn't discover class with name
> > classy (there's only spacy\classy) yet, so it tries to resolve all your "use"
> > statements (but you didn't write any) and then just gives you "classy", it
> > didn't throw any error just because it tried to delay autoloading of this class as far as
> > possible, if would do eval("return new $me;") then you would get fatal error.
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>