thgs/attributes-loader

收集PHP 8.x属性

v0.1 2022-03-06 17:31 UTC

This package is auto-updated.

Last update: 2024-09-06 22:51:45 UTC


README

attributes-loader

这个小型库旨在提供一种从类中加载PHP 8.x属性的方法。它仍在早期开发阶段。

使用方法

示例用法

<?php

namespace Thgs\AttributesLoader;

use Attribute;

#[Attribute()]
class TestAttribute
{
    public function __construct(private $where = null)
    {
    }

    public function getWhere()
    {
        return $this->where;
    }
}

#[TestAttribute(where: 'class')]
class TestSubject
{
    #[TestAttribute(where: 'method')]
    public function method()
    {
    }
}

$loader = new AttributesLoader();
$loader->fromClass(TestSubject::class);

/** @var TestAttribute[] $attributes */
$attributes = $loader->getAttributesCollected();

或者使用FluentAttributeCollector

<?php

$attributesCollected = FluentAttributeCollector::new()
    ->only([TestAttribute2::class])
    ->target(\Attribute::TARGET_PROPERTY)
    ->fromClass(TestSubject::class)
    ->getCollectedAttributes();

更多用法示例可以通过查看 测试 来找到。