Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组作为参数列表传递给类构造函数_Php_Class_Object - Fatal编程技术网

Php 将数组作为参数列表传递给类构造函数

Php 将数组作为参数列表传递给类构造函数,php,class,object,Php,Class,Object,我想创建一个类对象并将其参数作为数组传递 例如: $array = array ( 'param1' => 123, 'param1' => 456, 'param2' => 789 ); 必须转换为设置数组列表并传递给构造函数的: $a = new A(); 使用call\u user\u func\u数组 您可以使用反射 <?php class A { public function __c

我想创建一个类对象并将其参数作为数组传递

例如:

$array = array (
     'param1' => 123,
     'param1' => 456,
     'param2' => 789
);
必须转换为设置数组列表并传递给构造函数的:

$a = new A();

使用call\u user\u func\u数组


您可以使用反射

<?php
       class A {

              public function __construct ( ) {
                     print_r ( func_get_args ( ) ) ;
              }

       }

       $reflection = new ReflectionClass('A');
       $reflection->newInstanceArgs(array( 'a' , 'b' )) ;

构造函数被调用了两次,但我不需要它。
<?php
       class A {

              public function __construct ( ) {
                     print_r ( func_get_args ( ) ) ;
              }

       }

       $reflection = new ReflectionClass('A');
       $reflection->newInstanceArgs(array( 'a' , 'b' )) ;
<?php
class A
{
  public function __construct( $var=array() ) { print_r( $var ); }
}

$a=new A( array( 'foo','bar','baz' ) );
?>