While the example displays us what we can accomplish with attributes, it should be kept in mind that the main idea behind attributes is to attach static metadata to code (methods, properties, etc.).
This metadata often includes concepts such as "markers" and "configuration". For example, you can write a serializer using reflection that only serializes marked properties (with optional configuration, such as field name in serialized file). This is reminiscent of serializers written for C# applications.
That said, full reflection and attributes go hand in hand. If your use case is satisfied by inheritance or interfaces, prefer that. The most common use case for attributes is when you have no prior information about the provided object/class.
<?php
interface JsonSerializable
{
public function toJson() : array;
}
?>
versus, using attributes,
<?php
class JsonSerialize
{
public function __constructor(public ?string $fieldName = null) {}
}
class VersionedObject
{
public const version = '0.0.1';
}
public class UserLandClass extends VersionedObject
{
public string $myValue;
}
?>
The example above is a little extra convoluted with the existence of the VersionedObject class as I wished to display that with attribute mark ups, you do not need to care how the base class manages its attributes (no call to parent in overriden method).