Article Image

Php Attributes

Published on: Nov 07 2024

Attributes offer the ability to add structured, machine-readable metadata information on declarations in code: Classes, methods, functions, parameters, properties and class constants can be the target of an attribute. The metadata defined by attributes can then be inspected at runtime using the Reflection APIs. Attributes could therefore be thought of as a configuration language embedded directly into code. With attributes the generic implementation of a feature and its concrete use in an application can be decoupled. In a way it is comparable to interfaces and their implementations. But where interfaces and implementations are about code, attributes are about annotating extra information and configuration. Interfaces can be implemented by classes, yet attributes can also be declared on methods, functions, parameters, properties and class constants. As such they are more flexible than interfaces. A simple example of attribute usage is to convert an interface that has optional methods to use attributes. Let's assume an ActionHandler interface representing an operation in an application, where some implementations of an action handler require setup and others do not. Instead of requiring all classes that implement ActionHandler to implement a method setUp(), an attribute can be used. One benefit of this approach is that we can use the attribute several times.

<?php
interface ActionHandler
{
 public function execute();
}

#[Attribute]
class SetUp {}

class CopyFile implements ActionHandler
{
 public string $fileName;
 public string $targetDirectory;

 #[SetUp]
 public function fileExists()
 {
 if (!file_exists($this->fileName)) {
 throw new RuntimeException("File does not exist");
 }
 }

 #[SetUp]
 public function targetDirectoryExists()
 {
 if (!file_exists($this->targetDirectory)) {
 mkdir($this->targetDirectory);
 } elseif (!is_dir($this->targetDirectory)) {
 throw new RuntimeException("Target directory $this->targetDirectory is not a directory");
 }
 }

 public function execute()
 {
 copy($this->fileName, $this->targetDirectory . '/' . basename($this->fileName));
 }
}

function executeAction(ActionHandler $actionHandler)
{
 $reflection = new ReflectionObject($actionHandler);

 foreach ($reflection->getMethods() as $method) {
 $attributes = $method->getAttributes(SetUp::class);

 if (count($attributes) > 0) {
 $methodName = $method->getName();

 $actionHandler->$methodName();
 }
 }

 $actionHandler->execute();
}

$copyAction = new CopyFile();
$copyAction->fileName = "/tmp/foo.jpg";
$copyAction->targetDirectory = "/home/user";

executeAction($copyAction);



I've tried Harshdeeps example and it didn't run out of the box and I think it is not complete, so I wrote a complete and working naive example regarding attribute based serialization.

<?php
declare(strict_types=1);

#[Attribute(Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY)]
class JsonSerialize
{
 public function __construct(public ?string $fieldName = null) {}
}

class VersionedObject
{
 #[JsonSerialize]
 public const version = '0.0.1';
}

class UserLandClass extends VersionedObject
{
 protected string $notSerialized = 'nope';

 #[JsonSerialize('foobar')]
 public string $myValue = '';

 #[JsonSerialize('companyName')]
 public string $company = '';

 #[JsonSerialize('userLandClass')]
 protected ?UserLandClass $test;

 public function __construct(?UserLandClass $userLandClass = null)
 {
 $this->test = $userLandClass;
 }
}

class AttributeBasedJsonSerializer {

 protected const ATTRIBUTE_NAME = 'JsonSerialize';

 public function serialize($object)
 {
 $data = $this->extract($object);

 return json_encode($data, JSON_THROW_ON_ERROR);
 }

 protected function reflectProperties(array $data, ReflectionClass $reflectionClass, object $object)
 {
 $reflectionProperties = $reflectionClass->getProperties();
 foreach ($reflectionProperties as $reflectionProperty) {
 $attributes = $reflectionProperty->getAttributes(static::ATTRIBUTE_NAME);
 foreach ($attributes as $attribute) {
 $instance = $attribute->newInstance();
 $name = $instance->fieldName ?? $reflectionProperty->getName();
 $value = $reflectionProperty->getValue($object);
 if (is_object($value)) {
 $value = $this->extract($value);
 }
 $data[$name] = $value;
 }
 }

 return $data;
 }

 protected function reflectConstants(array $data, ReflectionClass $reflectionClass)
 {
 $reflectionConstants = $reflectionClass->getReflectionConstants();
 foreach ($reflectionConstants as $reflectionConstant) {
 $attributes = $reflectionConstant->getAttributes(static::ATTRIBUTE_NAME);
 foreach ($attributes as $attribute) {
 $instance = $attribute->newInstance();
 $name = $instance->fieldName ?? $reflectionConstant->getName();
 $value = $reflectionConstant->getValue();
 if (is_object($value)) {
 $value = $this->extract($value);
 }
 $data[$name] = $value;
 }
 }

 return $data;
 }

 protected function extract(object $object)
 {
 $data = [];
$reflectionClass = new ReflectionClass($object);
 $data = $this->reflectProperties($data, $reflectionClass, $object);
 $data = $this->reflectConstants($data, $reflectionClass);

 return $data;
 }
}

$userLandClass = new UserLandClass();
$userLandClass->company = 'some company name';
$userLandClass->myValue = 'my value';

$userLandClass2 = new UserLandClass($userLandClass);
$userLandClass2->company = 'second';
$userLandClass2->myValue = 'my second value';

$serializer = new AttributeBasedJsonSerializer();
$json = $serializer->serialize($userLandClass2);

var_dump(json_decode($json, true));