judahnator/trait-aware

使您的类了解它们使用的特性

v2.0.0 2020-10-10 10:46 UTC

This package is auto-updated.

Last update: 2024-09-11 02:49:24 UTC


README

pipeline status coverage report

这个包非常简单,允许您递归地找到类使用的所有特性。它受到了添加到这个评论上的php.net文档中的一个片段的启发。

安装

使用composer。

composer install judahnator/trait-aware

使用方法

这个库提供了一个函数,您可以使用它,它与class_uses()函数非常相似,但它包括父类和特性特性,是递归的。

您还可以“使用”一个特性 \judahnator\TraitAware\TraitAware。它提供了一个公共静态getTraits()方法,该方法将返回类使用的所有特性。

示例

trait foo 
{
}
trait bar
{
    use foo;
}
class baz
{
    use bar, \judahnator\TraitAware\TraitAware;
}
class bing extends baz
{
}

class_uses(baz::class);
// can see bar and the TraitAware traits, but not bar
[
    "bar" => "bar",
    "judahnator\TraitAware\TraitAware" => "judahnator\TraitAware\TraitAware",
];

class_uses(bing::class);
// can not see any traits
[];

judahnator\TraitAware\class_uses_deep(bing::class);
// can see foo, plus all the traits used by the baz class
[
    "foo" => "foo",
    "bar" => "bar",
    "judahnator\TraitAware\TraitAware" => "judahnator\TraitAware\TraitAware",
];

baz::getTraits();
// same as above but just the values
[
    "foo",
    "bar",
    "judahnator\TraitAware\TraitAware",
];