php 方法初始化默认值,PHP – 初始化具有大量参数和默认值的对象的最佳方式

我设计一个类,它定义了一个高度复杂的对象,其中包含大多数可选参数(其中许多都有默认值)(例如:$ 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,我真的只是寻找惯用/优雅的方式做到这一点。你的最佳做法是什么?

附录(有关此特定类的详细信息)

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

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

类当然有许多这些属性的getters和setter,所以用户可以覆盖它们;这个帖子的目的(和原始代码做的很好)是提供一个紧凑的方法实例化这些Form对象与必需的参数已设置。它实际上使得代码非常可读。

# Form Behaviour Parameters

# --------------------------

$self->{id}; # the id and the name of the

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

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

$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 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

);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值