Reading Attributes with the Reflection APITo access attributes from classes, methods, functions, parameters, properties and class constants, the Reflection API provides the method getAttributes on each of the corresponding Reflection objects. This method returns an array of ReflectionAttribute instances that can be queried for attribute name, arguments and to instantiate an instance of the represented attribute. This separation of reflected attribute representation from actual instance increases control of the programmer to handle errors regarding missing attribute classes, mistyped or missing arguments. Only after calling ReflectionAttribute::newInstance, objects of the attribute class are instantiated and the correct matching of arguments is validated, not earlier. Example #1 Reading Attributes using Reflection API
Instead of iterating all attributes on the reflection instance, only those of a particular attribute class can be retrieved by passing the searched attribute class name as argument. Example #2 Reading Specific Attributes using Reflection API
|