entire-studio/dynamic-accessors

Dynamic setters and getters. While it can be done, it doesn't mean you should do it.

v1.8.0 2024-12-28 16:57 UTC

README

Packagist Version (including pre-releases) GitHub release (latest SemVer including pre-releases) CI codecov

Dynamic setters and getters. While it can be done, it doesn't mean you should do it.

Installation

Install the latest version with

$ composer require entire-studio/dynamic-accessors

Examples

Basic - accessors on class properties

<?php

declare(strict_types=1);

use EntireStudio\DynamicAccessors\{
    DynamicAccessors,
    Get,
    Set
};

/**
 * You can annotate your class for IDE completion
 *
 * @method string|void firstName(?string $argument = '')
 * @method void setLastName(string $name)
 * @method string getLastName()
 */
class Basic
{
    use DynamicAccessors;

    #[Set, Get]
    private string $firstName;

    #[Set('setLastName'), Get('getLastName')]
    private string $lastName;
}

$basic = new Basic();
$basic->firstName('Clark');
$basic->setLastName('Kent');

printf(
    'My name is %s %s.' . PHP_EOL,
    $basic->firstName(),
    $basic->getLastName(),
);
$ php example/Basic.php

Constructor - accessors on constructor parameters

<?php

declare(strict_types=1);

use EntireStudio\DynamicAccessors\{
    DynamicAccessors,
    Get,
    Set
};

/**
 * You can annotate your class for IDE completion
 *
 * @method string|void firstName(?string $argument = '')
 * @method void setLastName(string $name)
 * @method string getLastName()
 */
class ConstructorPropertyPromotion
{
    use DynamicAccessors;

    public function __construct(
        #[Set, Get]
        private string $firstName,
        #[Set('setLastName'), Get('getLastName')]
        private string $lastName,
    ) {
    }
}

$cpp = new ConstructorPropertyPromotion(
    'Lois',
    'Lane'
);

printf(
    'My name is %s %s.' . PHP_EOL,
    $cpp->firstName(),
    $cpp->getLastName(),
);
$ php example/ConstructorPropertyPromotion.php

Commands

Development

  • composer test - runs test suite.
  • composer sat - runs static analysis.
  • composer style - checks codebase against PSR-12 coding style.
  • composer style:fix - fixes basic coding style issues.