felixdorn/property-accessor

使用优雅的助手读取不可访问的属性

1.0.1 2021-05-16 09:13 UTC

This package is auto-updated.

Last update: 2024-09-16 16:49:45 UTC


README

Tests Formats Version Total Downloads License

安装

需要 PHP 8.0.0+

您可以通过 composer 安装此包

composer require felixdorn/property-accessor

用法

use function \Felix\PropertyAccessor\access;

class Book {
    protected string $name = 'Harry Potter';
    protected int $isbn = 44081224;
}

$object = new Book();
access($object, 'isbn');  // returns 44081224 

// Properties are injected based on the parameter name passed to the function.
access($object, function ($isbn, $name) {
    return $isbn . ' - ' . $name;
}); // returns '44081124 - Harry Potter'

access($object, ['isbn', 'name']); // returns ['isbn' => 44081224, 'name' => 'Harry Potter']

测试

composer test

属性访问器Félix Dorn 创建,遵循 MIT 许可