PHP - 使用大量参数和默认值初始化对象的最佳方法

2024-04-29

我正在设计一个类,它定义一个高度复杂的对象,其中包含大量(50+)大部分可选参数,其中许多参数都有默认值(例如:$type = 'foo'; $width = '300'; $interactive = false;)。我试图确定设置构造函数和实例/类变量的最佳方法,以便能够:

  • 使类的使用变得容易
  • 使自动记录类变得容易(即:使用 phpDocumentor)
  • 优雅地编码

鉴于上述情况,我不想向构造函数传递大量参数。我将向它传递一个包含初始化值的哈希值,例如:$foo = new Foo(array('type'=>'bar', 'width'=>300, 'interactive'=>false));

在编写类代码方面,我仍然觉得我宁愿......

class Foo {
    private $_type = 'default_type';
    private $_width = 100;
    private $_interactive = true;

    ...
}

...因为我相信这将有助于文档生成(您可以获得类属性的列表,这让 API 用户知道他们必须使用哪些“选项”),并且“感觉”像是正确的方法它。

但是,然后您遇到了将构造函数中的传入参数映射到类变量的问题,并且在不利用符号表的情况下,您陷入了“蛮力”方法,这对我来说达不到目的(尽管我对其他方法持开放态度)意见)。例如。:

function __construct($args){
    if(isset($args['type'])) $_type = $args['type']; // yuck!
}

我考虑过创建一个单独的类变量,它本身就是一个关联数组。那么初始化就非常容易了,例如:

private $_instance_params = array(
    'type' => 'default_type',
    'width' => 100,
    'interactive' => true
);

function __construct($args){
    foreach($args as $key=>$value){
        $_instance_params[$key] = $value;
    }
}

但这似乎我没有利用私有类变量等本机功能,而且感觉文档生成不适用于这种方法。

感谢您阅读本文;我可能在这里问了很多,但我是 PHP 新手,我真的只是在寻找惯用/优雅的方法来做到这一点。您的最佳实践是什么?


Addendum(有关该特定类别的详细信息)

这个类很可能试图做太多事情,但它是一个用于创建和处理表单的旧 Perl 库的端口。可能有一种方法可以划分配置选项以利用继承和多态性,但实际上可能会适得其反。

根据要求,这里是一些参数的部分列表(Perl 代码)。您应该看到这些不能很好地映射到子类。

该类当然具有许多此类属性的 getter 和 setter,因此用户可以覆盖它们;这篇文章的目的(原始代码做得很好)是提供一种紧凑的方法来实例化这些具有已设置的所需参数的 Form 对象。它实际上使得代码非常可读。

# Form Behaviour Parameters
        # --------------------------
        $self->{id}; # the id and the name of the <form> tag
        $self->{name} = "webform"; # legacy - replaced by {id}
        $self->{user_id} = $global->{user_id}; # used to make sure that all links have the user id encoded in them. Usually this gets returned as the {'i'} user input parameter
        $self->{no_form}; # if set, the <form> tag will be omitted
        $self->{readonly}; # if set, the entire form will be read-only
        $self->{autosave} = ''; # when set to true, un-focusing a field causes the field data to be saved immediately
        $self->{scrubbed}; # if set to "true" or non-null, places a "changed" radio button on far right of row-per-record forms that indicates that a record has been edited. Used to allow users to edit multiple records at the same time and save the results all at once. Very cool.
        $self->{add_rowid}; # if set, each row in a form will have a hidden "rowid" input field with the row_id of that record (used primarily for scrubbable records). If the 'scrubbed' parameter is set, this parameter is also automatically set. Note that for this to work, the SELECT statement must pull out a unique row id. 
        $self->{row_id_prefix} = "row_"; # each row gets a unique id of the form id="row_##" where ## corresponds to the record's rowid. In the case of multiple forms, if we need to identify a specific row, we can change the "row_" prefix to something unique. By default it's "row_"

        $self->{validate_form}; # parses user_input and validates required fields and the like on a form
        $self->{target}; # adds a target window to the form tag if specified
        $self->{focus_on_field}; # if supplied, this will add a <script> tag at the end of the form that will set the focus on the named field once the form loads.
        $self->{on_submit}; # adds the onSubmit event handler to the form tag if supplied
        $self->{ctrl_s_button_name}; # if supplied with the name of the savebutton, this will add an onKeypress handler to process CTRL-S as a way of saving the form

        # Form Paging Parameters
        # ----------------------
        $self->{max_rows_per_page}; # when displaying a complete form using printForm() method, determines the number of rows shown on screen at a time. If this is blank or undef, then all rows in the query are shown and no header/footer is produced.
        $self->{max_pages_in_nav} = 7; # when displaying the navbar above and below list forms, determines how many page links are shown. Should be an odd number
        $self->{current_offset}; # the current page that we're displaying
        $self->{total_records}; # the number of records returned by the query
        $self->{hide_max_rows_selector} = ""; # hide the <select> tag allowing users to choose the max_rows_per_page
        $self->{force_selected_row} = ""; # if this is set, calls to showPage() will also clear the rowid hidden field on the form, forcing the first record to be displayed if none were selected
        $self->{paging_style} = "normal"; # Options: "compact"

当然,我们可以让自己陷入有关编程风格的更长时间的辩论中。但为了所有参与者的理智,我希望避免它!这里(又是 Perl 代码)是一个使用大量参数实例化该对象的示例。

my $form = new Valz::Webform (
            id                      => "dbForm",
            form_name               => "user_mailbox_recip_list_students",
            user_input              => \%params,
            user_id                 => $params{i},
            no_form                 => "no_form",
            selectable              => "checkbox",
            selectable_row_prefix   => "student",
            selected_row            => join (",", getRecipientIDsByType('student')),
            this_page               => $params{c},
            paging_style            => "compact",
            hide_max_rows_selector  => 'true',
            max_pages_in_nav        => 5
        );

我可以想到两种方法来做到这一点。如果你想保留实例变量,你可以迭代传递给构造函数的数组并动态设置实例变量:

    <?php

    class Foo {
        private $_type = 'default_type';
        private $_width = 100;
        private $_interactive = true;

        function __construct($args){
            foreach($args as $key => $val) {
                $name = '_' . $key;
                if(isset($this->{$name})) {
                    $this->{$name} = $val;
                }
            }
        }
    }

    ?>

使用数组方法时,您实际上不必放弃文档。只需在类主体中使用@property注释即可:

<?php

/**
 * @property string $type
 * @property integer $width
 * @property boolean $interactive
 */
class Foo {
    private $_instance_params = array(
        'type' => 'default_type',
        'width' => 100,
        'interactive' => true
    );

    function __construct($args){
        $this->_instance_params = array_merge_recursive($this->_instance_params, $args);
    }

    public function __get($name)
    {
        return $this->_instance_params[$name];
    }

    public function __set($name, $value)
    {
        $this->_instance_params[$name] = $value;
    }
}

?>

也就是说,具有 50 个成员变量的类要么仅用于配置(可以拆分),要么它做得太多,您可能需要考虑重构它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP - 使用大量参数和默认值初始化对象的最佳方法 的相关文章

随机推荐

  • 从 iBeacon 接收 BLE 信号到 Bluno(arduino with BLE)

    我想从 iBeacon 到 Bluno 接收 rssi 信号和 UUID Arduino 板具有 BLE 对此有一些疑问 有没有从 BLE 到 BLE 接收 UUID 和 rssi 的解决方案 两个BLE设备可以互相通信吗 我想找一些网站来
  • 检索 Couchbase 的所有记录(文档)

    我正在使用 node js 并寻找一种方法来获取特定的 couchbase 桶的所有文档 有没有没有循环和增量索引的解决方案 我知道我可以制作一个原子键 然后通过循环使用它来检索所有数据 但我需要一个返回所有文档的函数 是否有任何函数 至少
  • 自动播放视频的 canvas.drawimage 仅在视频元素可见时有效

    我试图通过将视频绘制到画布上来在视频上添加一些滤镜 问题是 当视频元素不在视图中时 它会停止绘制 理想情况下 我想将视频元素全部隐藏起来 我认为它只影响 Chrome 浏览器 另外 似乎如果您停止并用鼠标启动它 问题就会消失 functio
  • *ngIf 中的@ViewChild

    Question 最优雅的获取方式是什么 ViewChild显示模板中的相应元素后 下面是一个例子 还Plunker http plnkr co edit xAhnVVGckjTHLHXva6wp p preview可用的 组件 templ
  • mod_mono 在新安装的 centos 上出现 EOF 错误

    我全新安装了 Centos 6 3 已完全更新 我已经从源安装了 mono xsp 和 mod mono 每个包都完美编译 它们都以 usr local mono 前缀安装 因此所有内容都位于 usr local mono 下 我已将 In
  • IntelliJ & JRuby:如何设置项目?

    我已经下载了 IntelliJ 13 的试用版 并安装了适用于 Windows 的最新 JRuby 版本 我在网上进行了彻底搜索 但无法找到有关如何在 IntelliJ 中设置 JRuby 项目的任何指导 我选择了 IntelliJ 而不是
  • 修正增量函数的摊余成本

    因此 对于 n 位二进制字符串 A 0 n 1 其中 A 0 是最低有效位 A n 1 是最高有效位 增量算法为 Increment A n i 0 while i
  • 如何使用 FLUTTER go_router 弹出上下文?

    如何使用 flutter go router 返回上一个屏幕 如何弹出上下文 目前 我只是将一个新屏幕添加到堆栈中 无论我想返回还是前进 onTap gt context go secondPage 我用过 context pop 但它会抛
  • 在 Symfony2 中使用 json 数据水合实体

    有没有办法用 Symfony2 中传入请求的 json 数据来水合实体 我以为有类似的东西 Form bindRequest 但我找不到任何东西 如果能将这个功能与 knockout js 等库一起使用 那就太好了 如果您需要从 JSON
  • 从对话框调用 CustomAction 时出现 WiX 安装错误 2762

    我是初学者 开始学习WiX 我想在安装过程中捕获 验证和注册用户详细信息 我创建了一个对话框来捕获用户注册并在用户单击 下一步 后调用自定义操作 但在这里我收到安装程序错误 2762 虽然错误描述说 必须在 InstallInitializ
  • 通过 HTTP 更新自托管扩展

    我正在将基于 SDK 的 Firefox 扩展转换为 WebExtensions 并且遇到了更新扩展的问题 当前的扩展托管在我自己的域 这是一个 HTTP 域 上 以及更新 rdf file 现在 对于基于 SDK 的附加组件 只要使用以下
  • 未捕获的错误:地图容器已初始化

    我正在使用 React JS 制作网页 我的目标是在前端显示地图 我正在使用react leaflet npm 包来实现同样的目的 但是 我收到以下错误 Error Uncaught Error Map container is alrea
  • 如何防止通过“new”运算符分配类? (我想确保我的 RAII 类始终分配在堆栈上。)

    我想确保我的 RAII 类始终分配在堆栈上 如何防止通过 new 运算符分配类 您需要做的就是将类的 new 运算符声明为私有 class X private Prevent heap allocation void operator ne
  • 访问二维数组的一行末尾之后的元素是否是 UB?

    以下程序的行为是否未定义 include
  • Docker:内存文件系统

    我有一个 docker 容器 它对磁盘进行大量读 写操作 我想测试当我的整个 docker 文件系统都在内存中时会发生什么 我在这里看到一些答案说这不会是真正的性能改进 但这是为了测试 我想测试的理想解决方案是共享每个图像的公共部分 并在需
  • 是否可以将向量的一部分作为向量发送给函数? [复制]

    这个问题在这里已经有答案了 我想看看是否可以将向量的一部分传递给函数 以便它显示为函数的法线向量 更重要的是 我希望这可以在 O 1 的常数时间内完成 我不想迭代向量来创建一个新向量 事实上 我还希望在下面的示例中将新向量的大小更改为 40
  • 如何覆盖 Symfony2 包中的实体

    我在用着FOS用户包 for Symfony2 我需要能够注册一个用户不验证电子邮件是否唯一 我只需要一个有效的电子邮件 所以许多用户可以拥有相同的电子邮件 我知道这很奇怪 但我需要它 我在捆绑包中有一个实体 用户 扩展 FOSUserBu
  • 使用 JSeperator - Java 时出现异常间隙

    我一直在开发 Swing GUI 并在添加后出现一些不寻常和不需要的间隙JSeperator 知道如何删除它们吗 或者任何其他选择来很好地实现这一目标 视觉描述 之前差距就很明显了JLabel 速度 及之后JSlider 相关代码 cont
  • android:如何将图像添加到相册

    任何人都可以分享代码 或向我指出 Android 示例代码 来帮助我将图像添加到媒体商店 图库 中的相册中 在我的应用程序中 我从服务器下载图像 并使用相机 通过 Intent 拍摄新图像 我想将这些图像组织在特定于应用程序的相册中 类似于
  • PHP - 使用大量参数和默认值初始化对象的最佳方法

    我正在设计一个类 它定义一个高度复杂的对象 其中包含大量 50 大部分可选参数 其中许多参数都有默认值 例如 type foo width 300 interactive false 我试图确定设置构造函数和实例 类变量的最佳方法 以便能够