Dynamic setters and getters. While it can be done, it doesn't mean you should do it.
Install the latest version with
$ composer require entire-studio/dynamic-accessors
<?php
use EntireStudio\DynamicAccessors\{
DynamicAccessors,
Get,
Set
};
/**
* You can annotate your class for IDE completion
* @method void setLastName(string $name)
* @method string getLastName()
*/
class Example {
use DynamicAccessors;
#[Set, Get] // Register default accessors
private string $firstName;
#[Set('setLastName'), Get('getLastName')] // Register under different name
private string $lastName;
}
$e = new Example();
$e->firstName('Clark');
$e->setLastName('Kent');
printf(
'My name is %s %s.' . PHP_EOL,
$e->firstName(), // getter and setter have the same name
$e->getLastName() // getter is custom and different from setter
);
$ php example.php
composer lint
- run linter.composer lint:fix
- fix some issues detected by linter automatically (easy ones).composer test
- run tests.