Attributes and Interfaces

When we ask for a property's Attributes using $property->getAttributes() we can actually filter for only Attributes which are an instance of a particular class or interface. Cool!

This means that we can solve the problem of guaranteeing our rule Attribute has a getValidator() method by making it implement an interface with a getValidator() method, and then passing the $property->getAttributes() method the information it needs. In out case this will be an interface name and a flag: ReflectionAttribute::IS_INSTANCEOF.

This means that only Attributes which implement a ValidationRuleInterface will be retrieved and that they will all have a getValidator() method. Problem solved and we all live happily ever after 🏡

Branch: https://github.com/GaryClarke/learn-oophp/tree/122-attributes-and-interfaces

Complete and Continue  
Discussion

5 comments