reedware/is-attribute
Composer 安装命令:
composer require reedware/is-attribute
包简介
Adds truth test helper for checking if class is an attribute.
关键字:
README 文档
README
This package adds a truth test helper for checking if class is an attribute.
Introduction
PHP Attributes were introduced in PHP 8.0. From the documentation, you can see that Attributes are defined similar to classes:
#[Attribute] class SetUp { // }
However, if you're handed the name of a class, there's no good way to know if that class is a PHP Attribute or not. This is where the is_attribute() method, as defined by this package, comes in.
Installation
Install this package using Composer:
composer require reedware/is-attribute
Usage
function is_attribute(string|object|null $class, ?int $target = null, int $match = TARGET_MATCH_EQUALS): bool
Class Argument
Let's start with the basics. For more use-cases, you'll only need to pass one parameter to is_attribute(), being the class itself.
Here's an example of that:
echo is_attribute(SetUp::class); // true echo is_attribute(CopyFile::class); // false
Target Argument
If you care what targets the attributes support, this is where an optional second parameter comes in.
For example, you can define an attribute like so:
#[Attribute(Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY)] class Serialize { // }
This defines a Serialize attribute that can applied to a class or property.
To check if an attribute can be applied to a class or property, you can pass in targets as the second parameter to is_attribute:
echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY); // true
Note that checking one or the other will return false:
echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT); // false echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY); // false
When the second argument is not specified (e.g. null), the specified class must simply be an Attribute, regardless of target. This is different from passing in Attribute::TARGET_ALL as the second argument, which will require the attribute to specify all targets (which is the default).
echo is_attribute(Serialize::class, Attribute::TARGET_ALL); // false
This is because the second parameter must be an exact match to the attribute.
Match Argument
If you don't want to do an exact match, you can use the optional third parameter.
TARGET_MATCH_EQUALS
This is the default value of the third argument, and exhibits the behavior already described above.
TARGET_MATCH_INCLUDES
This match setting requires ALL of the provided targets to be included (e.g. and/conjunction).
echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT, TARGET_MATCH_INCLUDES); // true echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY, TARGET_MATCH_INCLUDES); // true echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER, TARGET_MATCH_INCLUDES); // false
TARGET_MATCH_ANY
This match setting requires ANY of the provided targets to be included (e.g. or/disjunction).
echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT, TARGET_MATCH_INCLUDES); // true echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY, TARGET_MATCH_INCLUDES); // true echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER, TARGET_MATCH_INCLUDES); // true
reedware/is-attribute 适用场景与选型建议
reedware/is-attribute 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「attributes」 「is-attribute」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 reedware/is-attribute 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 reedware/is-attribute 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 reedware/is-attribute 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
The last validation library you will ever need!
A PHPDoc docblock interpreter that is simple, easy to use, and provides Attribute alternatives.
Module with SP files for attribute_check
PSR-11 Container for Dependency Injection (auto-wiring, attributes, factories, closures, array access)
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-25