k-hei/lombok-like

使用原生函数 __call 自动获取和设置

v2.2.2 2017-03-01 01:32 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:14:43 UTC


README

你不需要在类中创建所有获取器和设置器。只需创建扩展 lombok-like 的属性,并在你的 IDE 自动完成中注释它即可。

版本

@2.1.0 - 抛出异常。

@2.0.0 - 删除抽象方法,将反射更改为闭包。

@1.2.1 - 将方法名称从 __get 更改为 get 以进行重写。

@1.2.0 - 移除对带下划线的代码的支持,并改进使用私有属性。

@1.1.0 - 适配旧代码,这些代码在属性名称之前使用了下划线。

入门

composer install

composer require k-hei/lombok-like

测试教程

在需要的地方包含 LombokLike 类

include 'vendor/autoload.php';

在所有需要的类上扩展它

class Test extends \LombokLike\BaseEntity {}

如果你希望方法可以在你的 IDE 中显示,请使用属性上的 PHPdoc

class Test extends \LombokLike\BaseEntity {
    ...
    /**
     * @method typeOfReturn getNameOfAttribute() optionally description
     * @method typeOfReturn setNameOfAttribute($value) optionally description
     */
    protected $nameOfAttribute;
    ...
}

然后使用!

$test = new Test();
$test->setNameOfAttribute("Test");
echo $test->getNameOfAttribute();
//Result display is: Test

如果你调用一个未知的方法,将收到 LombokException

$test->setUnknowAttr("Unkmow property");
  
Fatal error: Uncaught Call to undefined function: setUnknowAttr() In: F:\...\file.php On line: 99 
thrown in F:\...\LombokLike\entity\Base.php on line 100

作者

LombokLike 由 Khwesten Heiner 创建和维护。Heiner 是 MeuTutorTeckS 的资深 FullStack 网络开发者。

许可

LombokLike 在 MIT 公共许可证下发布。