DCOM-233: Support for class constant annotations #541

doctrinebot opened this Issue Feb 5, 2014 · 5 comments

2 participants


Jira issue originally created by user @deeky666:

I need to read annotations for class constants which currently is not possible. Obviously there is a good reason for that because PHP lacks a native ReflectionConstant class. Still sometimes this would be really useful and there are custom reflection implementation out there such as the PHP-Token-Reflection library (https://github.com/Andrewsville/PHP-Token-Reflection) which provide a class for that purpose.
Now the problem is that there is no way to extend the annotation reader to add support for this purpose without having to duplicate a lot of code. This is due to the annotation parser being private in the annotation reader and Doctrine not providing a Target::TARGET_CONSTANT.
If we could decide on supporting this, there are two options IMO:

  1. Provide a ReflectionConstant interface in the core and add getConstantAnnotation(ReflectionConstant $constant, $annotationName) and getConstantAnnotations(ReflectionConstant $constant) to the Reader interface in conjunction with an AbstractReader class to throw an exception when calling those methods (to preserve BC).

  2. Extend AnnotationReader to provide a final protected getParser() method for better extensibility so that one can subclass the annotation reader without heavy code duplication.

In both cases an addition of Target::TARGET_CONSTANT would desirable.

Currently I have to workaround this is in a very ugly manner by extending \ReflectionClass to access ReflectionConstant objects provided by PHP-Token-Reflection library and wrapping them into objects that emulate \ReflectionProperty, just to be able to use the annotation reader.

Do you think there is any solution to this which we can provide in Doctrine annotations lib?


Comment created by @ocramius:

[guilhermeblanco] was thinking of marking this as "won't fix". A constant, after all, is a constant, and it can be accessed no matter what.

[deeky666] can you clarify on the use-case? This is too much work for too little benefit IMO...


Comment created by @deeky666:

[~ocramius] This is not about access but about being able to put metadata on class constants. My exact use-case is an Enum implementation through class constants where I can put things like readable values, ordinal numbers and such on each enum constants. I haven't thought of any other special use-cases and it might not be as common to be able to enrich class constants with metadata but still it would perfectly fit the annotation reader.
I see that this might cause too much work so if you decide on a "won't fix" I'll be fine with that.


Comment created by stof:

Given that PHP does not expose the docComment of constants, I would vote for marking it as "won't fix", as it would force us to require a separate parsing of the code to find the docComment


Comment created by @guilhermeblanco:

No magic please... PHP is guilty here. We should propose docComment support there, then we add this support here.


Issue was closed with resolution "Won't Fix"

@doctrinebot doctrinebot closed this Dec 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment