Annotations in PHP

Sebastian Bergmann » 19 April 2005 » in PHP » 6 Comments

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:A usage example can be found here.
Defined tags for this entry: , ,

Trackback specific URI for this entry

6 Comments to "Annotations in PHP"

Display comments as (Linear | Threaded)
  1. David
    19/04/2005 at 09:52 Permalink
    I created a slightly different annotation implementation just a week ago (on an extension from the reflection classes, that allows for getDocComment() for properties). It works like this:

    /*
    * @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

  2. Brenden Vickery
    20/04/2005 at 23:41 Permalink
    Ive also done some work on annotation processing for an ORM Im working on. If you check the code out of cvs http://sourceforge.net/projects/perdure/, its in the tools/classes/ directory.

    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

  3. dgx
    04/10/2006 at 10:24 Permalink
    It seems that Zend Encoder (Zend Guard) discards ReflectionProperty::getDocComment(), even option --preserve-docs is enabled :-(

    Reply

  4. Antoine
    11/11/2008 at 10:50 Permalink
    Hi,

    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

  5. Marcelo Gornstein
    13/03/2011 at 21:28 Permalink
    Hello,

    I've made an experimental patch to ZendEngine/PHP (5.3.5) to allow native annotations in the PHP language, the code can be found here: https://github.com/marcelog/AnoForPHP

    It allows 0 or more annotations (without the use of DocComments) with 0 or more arguments themselves, in classes, methods, properties, AND arguments for methods, i.e:

    /*
    * The token chosen is "@@" because "@" was already taken.
    */
    @@Annotation(key="value1")
    class A {
    @@Annotation(key="value1")
    private $property;

    @@Annotation(key="value1")
    private function aMethod(@@Annotation(key="value1") @@Annotation2 $a, $b) {
    ...
    }
    }

    }

    Reply

  6. Rasmus Schultz
    17/05/2011 at 00:53 Permalink
    I have been working for almost a year on a new, open-source framework for annotations in PHP - the framework itself is now stable, so you're welcome to take a look:

    http://code.google.com/p/php-annotations/

    As said, the engine is stable, but I still have a lot of work to do - browse the Wiki for information about planned features, and the library of standard annotations. There is no forum yet, but if you have comments, feel free to drop me an email.

    Thanks!

    Reply

4 Trackbacks to "Annotations in PHP"

  1. p0t 20/04/2005 at 04:44
    Annotations in PHP - Sebastian Bergmann ...
  2. Sebastian Bergmann 21/09/2006 at 11:48
    The Extended Reflection API enhances PHP 5 by providing access to type information and annotations that are embedded as doc-comments into the sourcecode. Processable additions to the source code enable a wide range of new applications in general, and W
  3. Stubblog 10/11/2008 at 16:49
    Annotations are a really helpful feature in present-day development. An annotation is a special form of syntactic metadata that can be added to source code elements such as classes, methods, properties and parameters. They do not affect the program semant
  4. blog.monkeyz.eu 29/05/2011 at 12:39

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Submitted comments will be subject to moderation before being displayed.