antondperera / php-attributes-reader
简单的PHP属性读取/解析器
0.1.0
2024-03-09 18:23 UTC
Requires
- php: >=8.1
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-09 19:54:27 UTC
README
欢迎使用PHP Attributes Reader,这是一个轻量级且高效的库,旨在轻松提取和操作PHP 8中的类、方法和属性属性。随着PHP 8中属性的引入,该库简化了在代码库中读取和利用这些属性的过程。无论您是探索类、方法还是属性属性,PHP Attributes Reader都提供了一个用户友好的界面,以便无缝检索属性,无需直接处理PHP的Reflection API。通过这个直观且多功能的库,您可以轻松地利用PHP 8属性的强大功能,提升您的开发体验。
要求
PHP 8.1及更高版本。
安装
您可以通过Composer进行安装。运行以下命令
composer require antondperera/php-attributes-reader
请确保您使用的是Composer的自动加载
require_once 'vendor/autoload.php';
入门指南
示例代码和最简单的用法
具有类、方法和属性属性的PHP类
<?php declare(strict_types=1); #[TestAttribute1('testValue1')] class Abc { #[TestAttribute2('testValue2')] public ?string $test_property_1 = null; public ?string $test_property_2 = null; #[TestAttribute3('testValue3')] public function testMethod1() { // rest of the codes } public function testMethod2() { // rest of the codes } // rest of the codes }
读取这些属性的最简单方法
<?php declare(strict_types=1); $class = Abc::class; $attributes_reader = new \AntonDPerera\PHPAttributesReader\AttributesReader($class); // Class attributes echo $attributes_reader->hasClassAttributes(); // returns true var_dump($attributes_reader->getClassAttributes()); // returns list of Class attributes // Method attributes echo $attributes_reader->hasMethodAttributes(); // returns true echo $attributes_reader->hasMethodAttributes('testMethod2'); // returns false echo $attributes_reader->hasMethodAttributes('testMethod1'); // returns true var_dump($attributes_reader->getMethodAttributes('testMethod1')); // returns list of attributes for the given method. // Property attributes echo $attributes_reader->hasPropertyAttributes(); // returns true echo $attributes_reader->hasPropertyAttributes('test_property_2'); // returns false echo $attributes_reader->hasPropertyAttributes('test_property_1'); // returns true var_dump($attributes_reader->getPropertyAttributes('test_property_1')); // returns list of attributes for the given property.
有关更多详细信息,请参阅文档。
文档
请访问PHP Attributes Reader文档以获取关于所有方法和示例的完整指南,并学习如何使用它们。
限制
PHP属性有许多用途。当前版本中,PHP Attributes Reader涵盖了它们的一些简单用途。请参阅PHP Attributes Reader文档中的“限制”部分以获取更详细的指南。
贡献
有关如何为PHP Attributes Reader做出贡献的信息,请参阅贡献指南。