holyshared / attribute
Attribute for Hack
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
Language:Hack
Requires
- hhvm: >=3.6.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-10-15 15:53:32 UTC
README
Attribute for Hack
ClassAttribute
<?hh //strict namespace example; use attribute\ClassAttribute; final class Tag extends ClassAttribute { public function __construct( private string $name ) { } public function value() : string { return $this->name; } }
<?hh //strict namespace example; <<Tag('important')>> class TaggedTarget { }
<?hh //strict namespace example; $attribute = Tag::findByClassName(TaggedTarget::class); var_dump($attribute->value()); // Tag attribute instance $attribute = Tag::findByClass(new ReflectionClass(TaggedTarget::class)); var_dump($attribute->value()); // Tag attribute instance
MethodAttribute
<?hh //strict namespace example; use attribute\MethodAttribute; final class Tag extends MethodAttribute { public function __construct( private string $name ) { } public function value() : string { return $this->name; } }
<?hh //strict namespace example; class TaggedTarget { public function __construct( private string $name ) { } <<Tag('important')>> public function getName() : string { return $this->name; } }
<?hh //strict namespace example; $attributes = Tag::findByClassName(TaggedTarget::class); var_dump($attributes->at('getName')->value()); // important $attributes = Tag::findByClass(new ReflectionClass(TaggedTarget::class)); var_dump($attributes->at('getName')->value()); // important $attribute = Tag::findByMethod(new ReflectionMethod(TaggedTarget::class, 'getName')); var_dump($attribute->value()); // important
Run the test
composer install
composer test