# native php annotations

- Thread: https://externals.io/message/51673
- Original archive: https://news-web.php.net/php.internals/51673
- Started by Marcelo Gornstein on 2011-03-13T20:50:33+00:00
- Messages: 13
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #51673 — Marcelo Gornstein — 2011-03-13T20:50:33+00:00

URL: https://externals.io/message/51673#51673

Hello All,

I'm new to this list, so please excuse me if this subject was already
discussed, or proposed.

I've made an experimental patch to ZendEngine/PHP (5.3.5) to allow
native annotations (without doc comments), using the token @@ (the @
was already taken). It allows 0 or more annotations (with 0 or more
arguments each one) for classes, properties, methods, and arguments
for methods, like:

@@Annotation(key="value1")
class A {
    @@Annotation(key="value1")
    private $property;

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

The patch can be found at: https://github.com/marcelog/AnoForPHP.

The annotations and their optional arguments can be retrieved via the
getAnnotations() method of ReflectionClass, ReflectionMethod,
ReflectionParameter, and ReflectionProperty.

I plan to extend this patch with some extended functionality, like
having PHP instantiate a given class for the given annotation. By
using convention instead of configuration, the name of the annotation
is the name of the class, which should also extend some PHP base
class, like "Annotation".

I'm basically looking for comments on the patch and the functionality,
since I'm new to the ZendEngine as well.

Again, I'm sorry if this post should not go here or there is not
interest in such a feature in the language itself. Also, if I missed
something in the code of the patch, because it's more like a
proof-of-concept than a final formal proposal.

Regards!


--
// I don't sleep. I coffee.
"Make everything as simple as possible, but not simpler." -- Albert Einstein
"The class object inherits from Chuck Norris."
"Chuck Norris can divide by zero and can unit test an entire
application with a single assert."
"There’s a lot of work happening behind the scenes, courtesy of the
Spring AOP framework"
"Why do I have this nagging hunch that you have no idea what you're doing?"
"Any society that would give up a little liberty to gain a little
security will deserve neither and lose both" - Benjamin Franklin

---

## #51674 — Etienne Kneuss — 2011-03-13T21:33:56+00:00

URL: https://externals.io/message/51673#51674 · In reply to: #51673

> Hello All,
> 
> I'm new to this list, so please excuse me if this subject was already
> discussed, or proposed.

Hi,

it has already been discussed with length. Please take a look at:

http://wiki.php.net/rfc/annotations

it would be more productive if you explain in what ways your proposal
differs from the one above, and why that should be so.

Best,

> 
> I've made an experimental patch to ZendEngine/PHP (5.3.5) to allow
> native annotations (without doc comments), using the token @@ (the @
> was already taken). It allows 0 or more annotations (with 0 or more
> arguments each one) for classes, properties, methods, and arguments
> for methods, like:
> 
> @@Annotation(key="value1")
> class A {
>     @@Annotation(key="value1")
>     private $property;
> 
>     @@Annotation(key="value1")
>     private function aMethod(@@Annotation(key="value1") @@Annotation2 $a, $b) {
>         ...
>     }
> }
> 
> The patch can be found at: https://github.com/marcelog/AnoForPHP.
> 
> The annotations and their optional arguments can be retrieved via the
> getAnnotations() method of ReflectionClass, ReflectionMethod,
> ReflectionParameter, and ReflectionProperty.
> 
> I plan to extend this patch with some extended functionality, like
> having PHP instantiate a given class for the given annotation. By
> using convention instead of configuration, the name of the annotation
> is the name of the class, which should also extend some PHP base
> class, like "Annotation".
> 
> I'm basically looking for comments on the patch and the functionality,
> since I'm new to the ZendEngine as well.
> 
> Again, I'm sorry if this post should not go here or there is not
> interest in such a feature in the language itself. Also, if I missed
> something in the code of the patch, because it's more like a
> proof-of-concept than a final formal proposal.
> 
> Regards!
> 
> 
> --
> // I don't sleep. I coffee.
> "Make everything as simple as possible, but not simpler." -- Albert Einstein
> "The class object inherits from Chuck Norris."
> "Chuck Norris can divide by zero and can unit test an entire
> application with a single assert."
> "There’s a lot of work happening behind the scenes, courtesy of the
> Spring AOP framework"
> "Why do I have this nagging hunch that you have no idea what you're doing?"
> "Any society that would give up a little liberty to gain a little
> security will deserve neither and lose both" - Benjamin Franklin

---

## #51676 — Marcelo Gornstein — 2011-03-13T21:47:38+00:00

URL: https://externals.io/message/51673#51676 · In reply to: #51674

> it has already been discussed with length. Please take a look at:
>
> http://wiki.php.net/rfc/annotations
Thank you for the link. That is actually pretty much what I intended
to do. The only difference with my own version lies in the possibility
to annotate method parameters (very useful in many cases when using an
IoC/DI container). So that would be my 2 cents there.

I made the patch because it's a feature I wanted from a long time now,
and didn't find anything done about it. Do you know if this rfc is
going to be approved?

Thanks again for your quick reply,

---

## #51677 — Etienne Kneuss — 2011-03-13T21:52:22+00:00

URL: https://externals.io/message/51673#51677 · In reply to: #51676

> > […]
> Thank you for the link. That is actually pretty much what I intended
> to do. The only difference with my own version lies in the possibility
> to annotate method parameters (very useful in many cases when using an
> IoC/DI container). So that would be my 2 cents there.
> 
> I made the patch because it's a feature I wanted from a long time now,
> and didn't find anything done about it. Do you know if this rfc is
> going to be approved?

The main concern with the proposals above were:

1) the syntax

2) how to represent the data (i.e. objects)

3) if objects, when to instanciate them, when reflecting, once?

I don't believe the patch was anywhere near an accepted state back then,
sadly.

Best,

> 
> Thanks again for your quick reply,

---

## #51678 — Marcelo Gornstein — 2011-03-13T22:02:16+00:00

URL: https://externals.io/message/51673#51678 · In reply to: #51677

> I don't believe the patch was anywhere near an accepted state back then,
> sadly.
Are you saying there wont be annotations in PHP? Is there any way to
contribute to make this feature accepted and available?

---

## #51679 — Ben Schmidt — 2011-03-13T22:31:25+00:00

URL: https://externals.io/message/51673#51679 · In reply to: #51676

> > […]
> Thank you for the link. That is actually pretty much what I intended
> to do. The only difference with my own version lies in the possibility
> to annotate method parameters (very useful in many cases when using an
> IoC/DI container). So that would be my 2 cents there.

I haven't read the RFC, but what you suggested in your earlier email
looked good to me, and I like the idea, so I'd be in favour of doing
something. (I'm new here, too, and not a core dev or anything, so my
opinion doesn't and shouldn't carry much weight, but there it is.)

Ben.

---

## #51680 — Pierre Joye — 2011-03-13T23:07:38+00:00

URL: https://externals.io/message/51673#51680 · In reply to: #51678

> > […]
> Are you saying there wont be annotations in PHP? Is there any way to
> contribute to make this feature accepted and available?

No, only that no compromise has been reached on annotation support.


-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #51684 — guilhermeblanco@gmail.com — 2011-03-14T18:02:27+00:00

URL: https://externals.io/message/51673#51684 · In reply to: #51680

Hi all,

I promise myself to not revamp this discussion again, but it wasn't me
this time!

@Etienne: That RFC is outdated.
Since the last feedback form internals list, a lot of changes have
been made to that RFC. Maybe I should update it ASAP so you can
clearly understand what have changed to be compatible with current PHP
syntax.

If you are interested, Pierrick moved all the recent developments to a
github repository, which can be reached here:
https://github.com/adoy/PHP-Annotations

Take a look at some tests:
https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/parser_021.phpt
https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/ReflectionParameter_getAnnotations_003.phpt
https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/ReflectionClass_getAnnotations_004.phpt

Also, there's even an alternative patch that support positioned
parameters instead of named ones.
We just have to reach an agreement with what PHP core want.

@Marcelo: While your proposal looks very good, it lacks of the support
to nested Annotation.
Consider how userland/framework would use your idea. For example,
Symfony2 supports validation of data inside classes inspired on
JSR-303 (Bean Validation).

Symfony2 takes an advantage of a library Doctrine group (which I'm a
core member) created by parsing docblocks. When we created this
parser, I created this RFC with the good intention that PHP could
benefit of this known feature to enhance current userland
developments.
The first thing you need is your application still running ok with and
without comments. This already breaks all suggestions of creating a
PECL extension of docblock parser.



I'd like to see what people think about it and make something "IN" on
next PHP major version.



Cheers,


> 
> > […]
>
> No, only that no compromise has been reached on annotation support.
>
>
> --
> Pierre
>
> @pierrejoye | http://blog.thepimp.net | http://www.libgd.org
>
> --
> 
> 
>
>



-- 
Guilherme Blanco
Mobile: +55 (16) 9215-8480
MSN: guilhermeblanco@hotmail.com
São Paulo - SP/Brazil

---

## #51687 — Marcelo Gornstein — 2011-03-14T18:51:00+00:00

URL: https://externals.io/message/51673#51687 · In reply to: #51684

The presented patch is just a quick and dirty work that I could put
together in 2 days, with knowledge of the zend engine. Thanks to some
tips from Pierrick, I'm slowly improving its quality.

However my ideal goals are pretty close to what you have done in your
rfc. Today Pierrick added the possibility to annotate method
parameters just like in the patch I sent, so I'm happy I already
contributed with something ;)

I made the patch for similar reasons. I'm the author of Ding (IoC/DI
container) and I really dont like having to use doc blocks to support
annotations. I think this is an increasing need generally speaking in
the php world. If PHP is to be considered some kind of "agile"
language, it needs some kind of metadata handling (in this case,
annotations).

I think other things can be added to the rfc, like having some
annotations supported by php itself (@Init, @Destroy, @Configuration).
This would need to be instantiated and "run" as soon as the annotation
is detected. This could be accomplished by either php or c code, so
the behavior can be extended via pecl extensions and/or normal php
code.


On Mon, Mar 14, 2011 at 3:02 PM, guilhermeblanco@gmail.com
<guilhermeblanco@gmail.com> wrote:
> Hi all,
>
> I promise myself to not revamp this discussion again, but it wasn't me
> this time!
>
> @Etienne: That RFC is outdated.
> Since the last feedback form internals list, a lot of changes have
> been made to that RFC. Maybe I should update it ASAP so you can
> clearly understand what have changed to be compatible with current PHP
> syntax.
>
> If you are interested, Pierrick moved all the recent developments to a
> github repository, which can be reached here:
> https://github.com/adoy/PHP-Annotations
>
> Take a look at some tests:
> https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/parser_021.phpt
> https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/ReflectionParameter_getAnnotations_003.phpt
> https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/ReflectionClass_getAnnotations_004.phpt
>
> Also, there's even an alternative patch that support positioned
> parameters instead of named ones.
> We just have to reach an agreement with what PHP core want.
>
> @Marcelo: While your proposal looks very good, it lacks of the support
> to nested Annotation.
> Consider how userland/framework would use your idea. For example,
> Symfony2 supports validation of data inside classes inspired on
> JSR-303 (Bean Validation).
>
> Symfony2 takes an advantage of a library Doctrine group (which I'm a
> core member) created by parsing docblocks. When we created this
> parser, I created this RFC with the good intention that PHP could
> benefit of this known feature to enhance current userland
> developments.
> The first thing you need is your application still running ok with and
> without comments. This already breaks all suggestions of creating a
> PECL extension of docblock parser.
>
>
>
> I'd like to see what people think about it and make something "IN" on
> next PHP major version.
>
>
>
> Cheers,
>
> 
> > […]
>
>
>
> --
> Guilherme Blanco
> Mobile: +55 (16) 9215-8480
> MSN: guilhermeblanco@hotmail.com
> São Paulo - SP/Brazil
>



-- 

--
// I don't sleep. I coffee.
"Make everything as simple as possible, but not simpler." -- Albert Einstein
"The class object inherits from Chuck Norris."
"Chuck Norris can divide by zero and can unit test an entire
application with a single assert."
"There’s a lot of work happening behind the scenes, courtesy of the
Spring AOP framework"
"Why do I have this nagging hunch that you have no idea what you're doing?"
"Any society that would give up a little liberty to gain a little
security will deserve neither and lose both" - Benjamin Franklin

---

## #51742 — Karsten Dambekalns — 2011-03-24T08:14:42+00:00

URL: https://externals.io/message/51673#51742 · In reply to: #51684

Hi Guilherme.


> @Etienne: That RFC is outdated.
> Since the last feedback form internals list, a lot of changes have
> been made to that RFC. Maybe I should update it ASAP so you can
> clearly understand what have changed to be compatible with current PHP
> syntax.

I think having the RFC up-to-date is a must to get this rolling (again).
So, please, do it! :)

> I'd like to see what people think about it and make something "IN" on
> next PHP major version.

I honestly think I can speak for the whole FLOW3[1] developer and user
base in this regard. With that said, I would love to see native
annotation support in PHP!

If you need support and/or testing targets, get in touch!

Regards,
Karsten

[1] http://flow3.typo3.org/

---

## #51743 — Pierrick Charron — 2011-03-25T04:40:01+00:00

URL: https://externals.io/message/51673#51743 · In reply to: #51742

Hi Karsten,



> Hi Guilherme.
>
> 
> > […]
>
> I think having the RFC up-to-date is a must to get this rolling (again).
> So, please, do it! :)
>

As you may know, the wiki website is currently unavailable for an unknown
period. We can't update the RFC now but we'll do it for sure when the wiki
will be up and running again.


>
> > […]
>
> I honestly think I can speak for the whole FLOW3[1] developer and user
> base in this regard. With that said, I would love to see native
> annotation support in PHP!
>
> If you need support and/or testing targets, get in touch!
>
>
Like Guilherme said, you can start playing with the native annotation
support in PHP by applying the patch (
http://www.adoy.net/php/Annotations.php ) on trunk or you can also just do a
checkout of the git repository ( https://github.com/adoy/PHP-Annotations ).
You'll find some existing tests/examples in the tests/annotation folder.
Every feedback on this implementation is of course welcome and will help us
improve it so that it might be one day in PHP.


> Regards,
> Karsten
>
> [1] http://flow3.typo3.org/
>
> --
> 
> 
>
>
Cheers,
Pierrick

---

## #52175 — Ferenc Kovacs — 2011-05-09T18:32:09+00:00

URL: https://externals.io/message/51673#52175 · In reply to: #51684

On Mon, Mar 14, 2011 at 7:02 PM, guilhermeblanco@gmail.com <
guilhermeblanco@gmail.com> wrote:

> Hi all,
>
> I promise myself to not revamp this discussion again, but it wasn't me
> this time!
>
> @Etienne: That RFC is outdated.
> Since the last feedback form internals list, a lot of changes have
> been made to that RFC. Maybe I should update it ASAP so you can
> clearly understand what have changed to be compatible with current PHP
> syntax.
>
> If you are interested, Pierrick moved all the recent developments to a
> github repository, which can be reached here:
> https://github.com/adoy/PHP-Annotations
>
> Take a look at some tests:
>
> https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/parser_021.phpt
>
> https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/ReflectionParameter_getAnnotations_003.phpt
>
> https://github.com/adoy/PHP-Annotations/blob/master/tests/annotations/ReflectionClass_getAnnotations_004.phpt
>
> Also, there's even an alternative patch that support positioned
> parameters instead of named ones.
> We just have to reach an agreement with what PHP core want.
>
> @Marcelo: While your proposal looks very good, it lacks of the support
> to nested Annotation.
> Consider how userland/framework would use your idea. For example,
> Symfony2 supports validation of data inside classes inspired on
> JSR-303 (Bean Validation).
>
> Symfony2 takes an advantage of a library Doctrine group (which I'm a
> core member) created by parsing docblocks. When we created this
> parser, I created this RFC with the good intention that PHP could
> benefit of this known feature to enhance current userland
> developments.
> The first thing you need is your application still running ok with and
> without comments. This already breaks all suggestions of creating a
> PECL extension of docblock parser.
>
>
>
> I'd like to see what people think about it and make something "IN" on
> next PHP major version.
>
>
>
now that the wiki is back and this was brought up in the 5.4 release
planning, I think it would be a good idea to:
- update the RFC to be in sync with the implementation
- review the rfc and the patch itself
- make the necessary modifications if necessary
- decide whether we want this in 5.4 or not.

Tyrael

---

## #52177 — guilhermeblanco@gmail.com — 2011-05-09T18:38:10+00:00

URL: https://externals.io/message/51673#52177 · In reply to: #52175

Hi Ferenc,

I'll update the RFC to match the current implementation.
Pierrick is working to extract a diff more simplified so you can
quickly look at it.

Thanks.


>
>
> On Mon, Mar 14, 2011 at 7:02 PM, guilhermeblanco@gmail.com
> <guilhermeblanco@gmail.com> wrote:
> > […]
>
> now that the wiki is back and this was brought up in the 5.4 release
> planning, I think it would be a good idea to:
> - update the RFC to be in sync with the implementation
> - review the rfc and the patch itself
> - make the necessary modifications if necessary
> - decide whether we want this in 5.4 or not.
> Tyrael



-- 
Guilherme Blanco
Mobile: +55 (16) 9215-8480
MSN: guilhermeblanco@hotmail.com
São Paulo - SP/Brazil
