Hi Rasmus,

I think you may be onto something here... :)


On 28/06/2016 17:31, Rasmus Schultz wrote:
The UnknownAnnotation object has three properties: class-name,
method-name ("__construct" if constructor was used) and the list of
arguments.

I'm not sure we need an extra UnknownAnnotation class. It seems like all of that information could be available on the ReflectionAnnotation object, so you can get it without the class being available if you want to.

If the idea is to lazily-evaluate the arguments to the annotation, that could be handled by storing the AST and only evaluating them when you call either $annotation->getInstance() or $annotation->getArguments():

use MyAnnotations\FooAnnotation as Foo;
// Class alias resolved at compile time as normal
<< Foo(time()) >>
class A {}

$class_reflection = new ReflectionClass('A');
$annotations = $class_reflection->getAnnotations();
foreach ($annotations as $annotation) {
   if ( $annotation->getClassName() == 'MyAnnotations\\FooAnnotation' ) {
       var_dump( $annotation->getArguments() ); // `time()` evaluated here
var_dump( $annotation->getInstance() ); // `new MyAnnotations\FooAnnotation(time())` evaluated here
   }
}

It's not the responsibility of the ReflectionAnnotation class to worry about calling the autoloader, issuing errors, etc, it should just delegate that to the engine like any other constructor call. There's no need for an extra $annotation->classExists() method, either, because you can just say "class_exists($annotation->getClassName())".


Interestingly, this same delayed evaluation of arguments opens us up to include direct access to the AST as some (Nikita?) have asked for:

// Constructor argument can be any valid expression,
// however nonsensical it would be to evaluate it directly
<< Pre($a >= 42) >>
function foo($a) {}

$r = new ReflectionFunction('foo');
$annotations = $r->getAnnotations();
foreach ( $annotations as $annotation ) {
   if ( $annotation->getClassName() == 'Pre' ) {
       $ast = $annotation->getArgumentAST();
// Now we can do funky things with the AST, like evaluating it in a custom context :)
   }
}


Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to