felixdorn/property-accessor

Read an unaccessible property with a nice helper

1.0.1 2021-05-16 09:13 UTC

This package is auto-updated.

Last update: 2024-12-16 17:27:19 UTC


README

Tests Formats Version Total Downloads License

Installation

Requires PHP 8.0.0+

You can install the package via composer:

composer require felixdorn/property-accessor

Usage

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']

Testing

composer test

Property Accessor was created by Félix Dorn under the MIT license.