通过PHP5中的func_get_args()引用传递变量?
发布时间:2020-12-13 13:15:00 所属栏目:PHP教程 来源:网络整理
导读:我现在有一个类的方法/函数在这种形式: function set_option($content,$opt,$key,$val){ //...Some checking to ensure the necessary keys exist before the assignment goes here. $content['options'][$key][$opt] = $val;} 现在,我正在调查该功能,使第
我现在有一个类的方法/函数在这种形式:
function set_option(&$content,$opt,$key,$val){ //...Some checking to ensure the necessary keys exist before the assignment goes here. $content['options'][$key][$opt] = $val; } 现在,我正在调查该功能,使第一个参数可选,让我只传递3个参数.在这种情况下,使用类属性内容来代替我省略的内容. 首先要注意的是使用func_num_args()& func_get_args()与此结合,类似于: function set_option(){ $args = func_get_args(); if(func_num_args() == 3){ $this->set_option($this->content,$args[0],$args[1],$args[2]); }else{ //...Some checking to ensure the necessary keys exist before the assignment goes here. $args[0]['options'][$args[1]][$args[2]] = $args[3]; } } 我如何指定我传递第一个参数作为参考? (我使用PHP5,因此指定变量通过函数调用引用传递不是我最好的选择之一) (我知道我可以修改参数列表,以便最后一个参数是可选的,像函数set_option($opt,$val,& $cont = false)这样做,但是如果通过,我很好奇参考可以结合上面的函数定义,如果我宁愿使用它)
在函数声明中没有参数列表,没有办法将参数用作参考.你需要做的就是这样
function set_option(&$p1,$p2,$p3,$p4=null){ if(func_num_args() == 3){ $this->set_option($this->content,$p1,$p3); }else{ $p1['options'][$p2][$p3] = $p4; } } 因此,根据func_num_args()的结果,解释每个参数的真正含义. 相当丑陋,并使代码,你不想以后维护:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |