Php 如何使用动态创建的参数调用静态类方法?
我正在使用jQuery插件dataTables在网站上创建一个可编辑表,并尝试调用fields方法动态接受参数:Php 如何使用动态创建的参数调用静态类方法?,php,class,methods,static,datatables,Php,Class,Methods,Static,Datatables,我正在使用jQuery插件dataTables在网站上创建一个可编辑表,并尝试调用fields方法动态接受参数: Editor::inst( $db, $table, $primary_key ) ->fields( // this part needs to be dynamic Field::inst( 'value1' ) ->validator( 'Validate::numeric' ), Fi
Editor::inst( $db, $table, $primary_key )
->fields(
// this part needs to be dynamic
Field::inst( 'value1' )
->validator( 'Validate::numeric' ),
Field::inst( 'value2' )
->validator( 'Validate::numeric' )
)
->process( $_POST )
->json();
数组中有value1和value2,但可以有两个以上。数字取决于我要创建的表:
$columns = array("value1", "value2");
如何在方法调用中获取它们,以便正确地计算所有内容
我真的不想使用这个用例。必须有更好的方法来做到这一点。您可以使用和函数来处理动态参数
你可以这样使用它们
function yourFunction() {
$totalParams=func_num_args();
echo "Total parameters: ".$totalParams;
$params = func_get_args();
foreach($params as $i=>$param) {
// $param is passed value.
// $i is the index.
}
}
yourFunction('param1value','param2value','param3value');
就这样叫函数吧
function yourFunction() {
$totalParams=func_num_args();
echo "Total parameters: ".$totalParams;
$params = func_get_args();
foreach($params as $i=>$param) {
// $param is passed value.
// $i is the index.
}
}
yourFunction('param1value','param2value','param3value');
func\u num\u args()返回传递给函数的参数数。func\u get\u args()返回包含函数参数列表的数组 可以使用和函数处理动态参数 你可以这样使用它们
function yourFunction() {
$totalParams=func_num_args();
echo "Total parameters: ".$totalParams;
$params = func_get_args();
foreach($params as $i=>$param) {
// $param is passed value.
// $i is the index.
}
}
yourFunction('param1value','param2value','param3value');
就这样叫函数吧
function yourFunction() {
$totalParams=func_num_args();
echo "Total parameters: ".$totalParams;
$params = func_get_args();
foreach($params as $i=>$param) {
// $param is passed value.
// $i is the index.
}
}
yourFunction('param1value','param2value','param3value');
func\u num\u args()返回传递给函数的参数数。func\u get\u args()返回包含函数参数列表的数组
从datatables文档中 fields()方法可以接受您想要定义的任意多个字段实例,也可以多次调用以添加其他字段
$columns=array('value1','value2');
$editor=editor::inst($db,$table,$primary_key);
foreach($列作为$字段){
$editor->fields(Field::inst($Field)->validator('Validate::numeric');
}
$editor->process($\u POST)
->json();
您还可以使用以下内容使验证器动态:
$columns=array('value1'=>'Validate::numeric','value2'=>'Validate::notEmpty');
$editor=editor::inst($db,$table,$primary_key);
foreach($field=>$validator的列){
$editor->fields(Field::inst($Field)->validator($validator));
}
$editor->process($\u POST)
->json();
来自datatables文档
fields()方法可以接受您想要定义的任意多个字段实例,也可以多次调用以添加其他字段
$columns=array('value1','value2');
$editor=editor::inst($db,$table,$primary_key);
foreach($列作为$字段){
$editor->fields(Field::inst($Field)->validator('Validate::numeric');
}
$editor->process($\u POST)
->json();
您还可以使用以下内容使验证器动态:
$columns=array('value1'=>'Validate::numeric','value2'=>'Validate::notEmpty');
$editor=editor::inst($db,$table,$primary_key);
foreach($field=>$validator的列){
$editor->fields(Field::inst($Field)->validator($validator));
}
$editor->process($\u POST)
->json();
您确定这对我的特定用例有用吗?我不能这样调用函数,因为我不知道会有多少个参数。正如我提到的,这两个函数不依赖于调用函数的方法。它可以帮助您在不知道实际数字的情况下获取传递给函数的所有参数。在函数中使用func_get_args(),使用foreach访问所有参数,并使用它们与查询或其他任何内容绑定。func_get_args():从全局范围调用-无函数上下文
…如果列出了相同的错误,这可能有助于您解决它。您确定这对我的特定用例有用吗?我不能这样调用函数,因为我不知道会有多少个参数。正如我提到的,这两个函数不依赖于调用函数的方法。它可以帮助您在不知道实际数字的情况下获取传递给函数的所有参数。在函数中使用func_get_args(),使用foreach访问所有参数,并使用它们与查询或其他任何内容绑定。func_get_args():从全局范围调用-无函数上下文
…这可能有助于解决列出的相同错误。