mercredi 3 février 2021

Is it possible to implement an interface with PHP 8 attribute and check if it is an instance of that interface?

I have an PHP attribute as follow:

#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_METHOD|Attribute::TARGET_PARAMETER)]
class NotNull implements Constraint{
 ...
}

Now, I want to get the list of all attributes of a property and check if they are instance of Constraint:

$propertyReflection = ...;
$attributes = $propertyReflection->getAttributes();
foreach($attributes as $attribute){
    if($attribute instanceof Constraint){
         // do something
    }
}

However, it do nothing?

So, my question is: is it possible to implement an interface with an attribute and then check if the attribute is an instance?





Aucun commentaire:

Enregistrer un commentaire