Getters and setters v1.0.1
#[Getter] and #[Setter] accessors from lombok-php on AbstractWith.
AbstractWith extends \Lombok\Helper. #[Getter] and #[Setter] come from marcin-orlowski/lombok-php, not from Dev Tools. This guide covers the integration only.
Full attribute contract: lombok-php attributes.
Quick start
use Lombok\Getter;
use Lombok\Setter;
use Devcraft\Abstracts\AbstractWith;
use Devcraft\Attributes\With;
#[Getter]
final class Query extends AbstractWith
{
#[With]
private ?int $page = null;
#[Setter]
private bool $visible = false;
}
$query = (new Query())->withPage(2);
$query->getPage(); // 2
$query->setVisible(true);
$query->isVisible(); // truewithPage() goes to WithHandler. getPage() / setVisible() / isVisible() go to Lombok.
Class vs property
Put the attributes on the class or on a property.
- On the class: accessors for every eligible property.
publicandstaticare skipped silently.#[Setter]also skipsreadonlyproperties. - On a property: that property only.
public/static/readonly(for Setter) throw.
If a property already has #[Getter] or #[Setter], the class-level scope is not applied to that property. Use that to narrow accessors:
use Lombok\Getter;
use Lombok\Setter;
#[Setter, Getter]
final class Entity extends AbstractWith
{
#[Getter]
private int $id = 0;
private ?string $name = null;
}$name gets getName() and setName(). $id gets getId() only.
#[With] / #[WithItem] are not Lombok accessor attributes. Class-level #[Getter] coexists with #[With] on the same property.
Naming
Names are StudlyCase of the property: $starting_after → getStartingAfter() / setStartingAfter().
When the property's only type is bool, the getter is isVisible(), not getVisible(). A union that includes bool uses plain get.
set* returns $this for chaining.
Constructor
Helper calls Lombok::construct($this) from __construct(). If a subclass declares its own constructor:
public function __construct()
{
parent::__construct();
}Without parent::__construct(), with* still works immediately. Getters and setters attach on the first Lombok __call (or not at all until you call get* / set*).
For a readonly class, lombok requires \Lombok\HelperReadonly, not Helper. AbstractWith extends the regular Helper, so readonly classes are out of scope. Readonly properties are skipped by class-level Setter.
Limitations
- Magic methods cannot satisfy interface methods.
- IDEs will not see
get*/set*unless you add@methodPHPDoc. - A name collision with a real method: on a property, an exception; on the class, the original method stays and no accessor is added.
See also: AbstractWith, With attributes.