Php 将动态值从一个函数传递到类中的另一个函数

Php 将动态值从一个函数传递到类中的另一个函数,php,Php,嗨,我在一个类中有一个函数,它将打印这个函数的所有变量传递 类别代码: <?php class MyPrintClass { public function printVaraible() { var_dump(func_get_args()) } } $printVar = new MyPrintClass; ?> <?php class MyPrintClass { public function printVaraible($t

嗨,我在一个类中有一个函数,它将打印这个函数的所有变量传递

类别代码:

<?php
 class MyPrintClass {
   public function printVaraible() {
     var_dump(func_get_args())
   }
 }

 $printVar = new MyPrintClass;
?>
<?php
  class MyPrintClass {
    public function printVaraible($tag,$value) {
      echo $tag.' == '.$value;
      var_dump(func_get_args());
    }
  }

  $printVar = new MyPrintClass;
?>
我得到的输出高于cmd

array (size=1)
  0 => string 'value1' (length=6)
  1 => string 'value2' (length=6)
  2 => string 'value3' (length=6)
如果我使用like blow将其显示为单个数组。。我想把这个值分开。。怎么可能呢

function getVal(){
  global $printVar;
  $print->printVaraible(func_get_args());
}
我需要像下面这样传递值

getVal('value1','Value2',20);
我需要输出为

array (size=1)
  0 => string 'value1' (length=6)
  1 => string 'value2' (length=6)
  2 => string 'value3' (length=6)
目前,我得到NULL作为输出

根据deceze给出的答案更新了问题 **我的代码也有一个小小的变化** 类别代码:

<?php
 class MyPrintClass {
   public function printVaraible() {
     var_dump(func_get_args())
   }
 }

 $printVar = new MyPrintClass;
?>
<?php
  class MyPrintClass {
    public function printVaraible($tag,$value) {
      echo $tag.' == '.$value;
      var_dump(func_get_args());
    }
  }

  $printVar = new MyPrintClass;
?>

通过将类转换为函数

<?php
function getVal($tag,$value) {
 global $printVar;
 call_user_func_array([$printVar, 'printVariable'], $tag,$value,func_get_args());
}

如果我尝试按以下方式使用,则会出现错误

<?php getVal('first','second','third,'fourth'); ?>


这将使用数组中的单独参数调用函数。不过,我怀疑这是否有用,因为不管怎样,您最终将得到一个来自
func\u get\u args
的数组。为什么不首先传递数组呢?

您的返回值会变为null,因为您从未从上述函数返回任何内容

如果您想调用类方法,我建议您研究Refection,也可以这样做

$a = 'printVaraible';

$class->$a(1,3); //
除了使用call_user_func_数组之外,我还建议您研究闭包(php>5.3),这样您就可以这样编写闭包了

$a = function foo(){ echo 'foo'};
$a();

最后一件事我通常避免使用global,因为它在代码中隐藏了值的来源。最好是将值注入函数范围,而不是将其与全局值混合,在一个大型项目中,我只是不惜一切代价避免它们,忘记它们的存在。

您使用的是
global$printVar
,但随后请参考
$print
@deceze它的输入错误,但我得到了输出frm您的答案我使用了这个,但我得到返回null作为值当我调用类中的函数时,我得到null作为返回值请检查我的更新问题谢谢您的回答,因为您告诉我添加
返回函数
。它起作用了