我如何从类||调用函数具有变量和传递参数的特征?

时间:2015-11-11 15:24:32

标签: class oop arguments traits

我的类结构如何(lib.php):

trait smartMethods{    

     function Numbers($length,$position){

           echo $length.$position."\n";

           }

       }


class smartBrute  {


Private $config,$smlist;
use smartMethods;


function init(){

foreach($this->config as $k => $v){

    $inp = "";
    foreach($v as $k2 => $v2){
    ($inp == "" ? $inp = $v2 : $inp .= ",".$v2); 
    }



         $this->{$k}($inp); // Here Is Problem

    }


} //EO init

} //EO Class

调用类(init.php):

$options["Numbers"] = Array(2,1); 
$brute = new smartBrute($options);

$这 - > {$ķ}($ INP); < - 这是我的问题函数数字需要2个参数。没有像这样的论证$ this-> {$ k}()它有效,但我无法理解如何传递这种情况参数。

1 个答案:

答案 0 :(得分:0)

我找到了这种解决方案,

function init(){

     foreach($this->config as $k => $v){


     call_user_func_array(array($this,$k),$v); // call trait Method funcs


}


}

我用call_user_func_array这个函数传递参数