MagicGetterSetterTrait
Automatically define getter/setter methods for public and protected fields.
BASIC USAGE
- Choose a class
- Add the trait (
use MagicGetterSetterTrait;). - Add a public or protected property (
protected $fooBar;). - When using the class, you may now call
setFooBar($value)andgetFooBar().
TIPS AND TRICKS
- To provide better hints/DX in IDEs, you may add the
@methodnotations to the class docblock. There are several examples of this in APIv4 (see e.g.AbstractAction.phporAbstractQueryAction.php). - When/if you need to customize the behavior of a getter/setter, then simply add your own method. This takes precedence over magic mehods.
- If a field name begins with
_, then it will be excluded.
Table of Contents
Methods
- __call() : static|mixed
- Magic function to provide getters/setters.
- getMagicProperties() : array<string|int, mixed>
- Get a list of class properties for which magic methods are supported.
Methods
__call()
Magic function to provide getters/setters.
public
__call(string $method, array<string|int, mixed> $arguments) : static|mixed
Parameters
- $method : string
- $arguments : array<string|int, mixed>
Tags
Return values
static|mixedgetMagicProperties()
Get a list of class properties for which magic methods are supported.
protected
static getMagicProperties() : array<string|int, mixed>
Return values
array<string|int, mixed> —List of supported properties, keyed by property name. Array(string $propertyName => bool $true).