marcin-orlowski/lombok-php

Write less code!

1.1.1 2023-10-10 18:45 UTC

This package is auto-updated.

Last update: 2024-10-30 01:37:32 UTC


README

Lombok PHP - write less code!

Latest Stable Version codecov License

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