Skip to content

Instantly share code, notes, and snippets.

@nicoverbruggen
Created August 30, 2023 09:46
Show Gist options
  • Save nicoverbruggen/f6eaeca0307f14289b6c2d0195cab6c7 to your computer and use it in GitHub Desktop.
Save nicoverbruggen/f6eaeca0307f14289b6c2d0195cab6c7 to your computer and use it in GitHub Desktop.
Thinking about an abstraction for accessing PHP attributes
<?php
use NicoVerbruggen\Attributes\ExampleAttribute;
use NicoVerbruggen\Objects\SampleObject;
$object = new SampleObject();
// Global helper style
get_attribute(ExampleAttribute::class, $object);
get_method_attribute(ExampleAttribute::class, $object, 'execute');
get_property_attribute(ExampleAttribute::class, $object, 'name');
...
// Helper (no trait required)
reflect($object)->getAttribute(ExampleAttribute::class);
reflect($object)->getMethodAttribute('execute', ExampleAttribute::class);
reflect($object)->getPropertyAttribute('name', ExampleAttribute::class);
...
// Fluent syntax (with Trait)
$object->getAttribute(ExampleAttribute::class);
$object->getMethodAttribute('execute', ExampleAttribute::class);
$object->getPropertyAttribute('name', ExampleAttribute::class);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment