tbn/getter-trait-bundle

3.00 2024-07-03 18:43 UTC

This package is auto-updated.

Last update: 2024-09-03 19:22:34 UTC


README

生成 trait 内部 getter 和 setter 的 Symfony Bundle。

为什么?基本的 getter 并不能提供关于类的信息。

类因为没有任何“业务”原因而变得很长。

只有重新定义的方法才能识别出具有业务目的的方法。

安装

composer require --dev "tbn/getter-trait-bundle"

config/bundles.php 文件中,将 bundle 添加到开发环境

return [
    ...
    tbn\GetterTraitBundle\GetterTraitBundle::class => ['dev' => true,],
];

使用

GetSetTrait 属性添加到您的类中

use tbn\GetterTraitBundle\Attributes\GetSetTrait; // add this line

#[GetSetTrait] // add this line
class MyClass
{
    private String $name;
}

运行 ./bin/console generate:getter:traits 命令

将在同一文件夹中自动生成一个新文件,包含 MyClass 类的 getter 和 setter。

当属性被修改或添加时,您必须重新运行命令以更新 traits。

使用生成的 trait。

#[GetSetTrait]
class MyClass
{
    use MyClassTrait; // add this line

    private String $name;
}

我需要重新定义方法

只需将生成的代码复制粘贴到您的类中,并根据您的需要进行更新。

更多示例

示例文件夹