Created
August 30, 2023 09:46
-
-
Save nicoverbruggen/f6eaeca0307f14289b6c2d0195cab6c7 to your computer and use it in GitHub Desktop.
Thinking about an abstraction for accessing PHP attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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