Annotations in PHP
Yesterday someone proposed adding support for annotations to the PHP Interpreter. Although it would be nice to have this kind of functionality out-of-the-box and built into the interpreter itself this is not necessary.
I could not resist and started working on a package that exends the Reflection API of PHP 5 by adding getAnnotations(), getAnnotation($name), and hasAnnotation($name) methods to ReflectionClass, ReflectionFunction, ReflectionMethod, and ReflectionProperty.
The syntax for annotations follows the standard for PHP Doc Comments and @foo bar declares an annotation of name "foo" with value "bar".
The inital code can be found here:
I could not resist and started working on a package that exends the Reflection API of PHP 5 by adding getAnnotations(), getAnnotation($name), and hasAnnotation($name) methods to ReflectionClass, ReflectionFunction, ReflectionMethod, and ReflectionProperty.
The syntax for annotations follows the standard for PHP Doc Comments and @foo bar declares an annotation of name "foo" with value "bar".
The inital code can be found here:
- Reflection_Annotation
- Reflection_Annotation_AnnotatedElement
- Reflection_Annotation_Class
- Reflection_Annotation_Function
- Reflection_Annotation_Method
- Reflection_Annotation_Parser
- Reflection_Annotation_Property
19/04/2005 at 09:52 Permalink
/*
* @SomeClass(a=>"B", b => 3, c => true)
*/
$extendedReflectionProperty->getAnnotation("SomeClass");
and returns an instance of SomeClass with the appropriate properties set (a,b,c). I'll send you my code / a patch when I get back home.
Reply
20/04/2005 at 23:41 Permalink
That code parses annotations that are closer to the java spec:
/**
*@Annotation(value=param-value)
*/
I would rather see annotation support in the php interpreter so that we dont need to put the annotations in a docblock. Something closer to the way java is going.
Reply
04/10/2006 at 10:24 Permalink
Reply
11/11/2008 at 10:50 Permalink
is it still possible to get the code of your implementation? Just to have a look on how is it possible to do it.
Antoine C.
Reply