marcin-orlowski / lombok-php
Write less code!
Installs: 7 771
Dependents: 1
Suggesters: 0
Security: 0
Stars: 28
Watchers: 4
Forks: 1
Open Issues: 4
Requires
- php: ^8.0|^8.1|^8.2
Requires (Dev)
- phpstan/phpstan: ^1.5
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-11-30 01:54:05 UTC
README
Table of contents
Introduction
Lombok PHP
is a package offering a growing set
of PHP attributes (a
feature introduced in PHP 8.0) designed to help reduce
boilerplate code in your project by providing commonly used functionality that can be easily
applied.
The main goal is to provide all the functionality at runtime, without any intermediate steps needed and without code generation.
And yes, the project name is shamelessly borrowed from the beloved Java's Project Lombok. However, it is not affiliated in any way; it's merely a nod to the scope and (target) functionality.
Benefits
- Less code to write and maintain,
- No more repetitive boilerplate code,
- Can coexist with other attributes (i.e., Doctrine, etc.),
- No code generation (all handled on-the-fly),
- No additional dependencies,
- Supports object cloning,
- Production ready.
Examples
Vanilla PHP:
class Entity { protected int $id; protected string $name; protected ?int $age; public function getId(): int { return $this->id; } public function getName(): string { return $this->name; } public function setName(string $name): static { $this->name = $name; return $this; } public function getAge(): ?int { return $this->age; } public function setAge(?int $age): static { $this->age = $age; return $this; } }
Using Lombok PHP
(inheritance from \Lombok\Helper
is helpful
but optional):
use Lombok\Getter; use Lombok\Setter; #[Setter, Getter] class Entity extends \Lombok\Helper { #[Getter] protected int $id; protected string $name; protected ?int $age; }
Click here to see setup instruction and all the technical details.
License
- Written and copyrighted ©2022-2023 by Marcin Orlowski <mail (#) marcinorlowski (.) com>
Lombok PHP
is open-sourced software licensed under the LGPL 3.0