maximo-perez-villalba/class-properties-reader

该类通过反射读取一个类的所有属性,包括其继承层次结构中的私有属性。

1.1.2 2022-11-28 18:38 UTC

This package is auto-updated.

Last update: 2024-09-28 22:26:19 UTC


README

该类通过反射读取一个类的所有属性,包括其继承层次结构中的私有属性。实现了类 ReflectionClass::getProperties 的扩展方法。

使用示例

class YourCustomClassRoot
{
    private string $propertyString = 'aValueOfTypeString';
    private int $propertyInt = 0;
    private float $propertyFloat = 0.0;
}

class YourCustomClassNode extends YourCustomClassRoot
{
    private bool $propertyBool = false;
    private array $propertyArray = [];
}

class YourCustomClassLeaf extends YourCustomClassNode
{
    private object ?$propertyObjectOrNull = null;
}

如何使用

调用 ClassPropertiesReader::getAll(string $classname) 方法,返回一个 ReflectionProperty[] 数组。

$allProperties = ClassPropertiesReader::getAll(YourCustomClassLeaf::class);
print_r($allProperties);

输出

Array
(
    [propertyObjectOrNull] => ReflectionProperty Object
        (
            [name] => propertyObjectOrNull
            [class] => YourCustomClassLeaf
        )

    [propertyArray] => ReflectionProperty Object
        (
            [name] => propertyArray
            [class] => YourCustomClassNode
        )

    [propertyBool] => ReflectionProperty Object
        (
            [name] => propertyBool
            [class] => YourCustomClassNode
        )

    [propertyFloat] => ReflectionProperty Object
        (
            [name] => propertyFloat
            [class] => YourCustomClassRoot
        )

    [propertyInt] => ReflectionProperty Object
        (
            [name] => propertyInt
            [class] => YourCustomClassRoot
        )

    [propertyString] => ReflectionProperty Object
        (
            [name] => propertyString
            [class] => YourCustomClassRoot
        )
)