【问题标题】:How would I call a function from a class || trait with a variable and pass arguments?我如何从一个类中调用一个函数 ||带有变量的特征并传递参数?
【发布时间】:2016-02-12 17:42:00
【问题描述】:

这就是我的类结构的样子(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);

$this->{$k}($inp); {$k}() 它可以工作,但我不明白如何在这种情况下传递参数。

【问题讨论】:

    标签: class oop arguments traits


    【解决方案1】:

    我找到了这种解决方案,

    function init(){
    
         foreach($this->config as $k => $v){
    
    
         call_user_func_array(array($this,$k),$v); // call trait Method funcs
    
    
    }
    
    
    }
    

    我用call_user_func_array这个函数传递了参数

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多