Creating a dynamic class instance programmatically in PHP with variable arguments?

佐手、 提交于 2021-01-27 02:57:25

问题


I've some code that creates ad instance with a dynamic class (i.e. from a variable):

$instance = new $myClass();

Since the constructor has different argument count depending on $myClass value, How do I pass a variable list of arguments to the new statement? Is it possible?


回答1:


class Horse {
    public function __construct( $a, $b, $c ) {
        echo $a;
        echo $b;
        echo $c;
    }
}

$myClass = "Horse";
$refl = new ReflectionClass($myClass);
$instance = $refl->newInstanceArgs( array(
    "first", "second", "third"    
));
//"firstsecondthird" is echoed

You can also inspect the constructor in the above code:

$constructorRefl = $refl->getMethod( "__construct");

print_r( $constructorRefl->getParameters() );

/*
Array
(
    [0] => ReflectionParameter Object
        (
            [name] => a
        )

    [1] => ReflectionParameter Object
        (
            [name] => b
        )

    [2] => ReflectionParameter Object
        (
            [name] => c
        )

)
*/



回答2:


The easiest route would be to use an array.

public function __construct($args = array())
{
  foreach($array as $k => $v)
  {
    if(property_exists('myClass', $k)) // where myClass is your class name.
    {
      $this->{$k} = $v;
    }
  }
}



回答3:


I don't know why but i don't like using the new operator in my code.

Here is a static function to create an instance of a class called statically.

class ClassName {
    public static function init(){       
        return (new ReflectionClass(get_called_class()))->newInstanceArgs(func_get_args());        
    }

    public static function initArray($array=[]){       
        return (new ReflectionClass(get_called_class()))->newInstanceArgs($array);        
    }

    public function __construct($arg1, $arg2, $arg3){
        ///construction code
    } 
}

If you are using it inside a namespace you need to escape ReflectionClass like so: new \ReflectionClass...

Now you can call the init() method with a variable number of arguments and it will pass it on to the constructor and return an object for you.

Normal way using new

$obj = new ClassName('arg1', 'arg2', 'arg3');
echo $obj->method1()->method2();

Inline way using new

echo (new ClassName('arg1', 'arg2', 'arg3'))->method1()->method2();

Static call using init instead of new

echo ClassName::init('arg1', 'arg2', 'arg3')->method1()->method2();

Static call using initArray instead of new

echo ClassName::initArray(['arg1', 'arg2', 'arg3'])->method1()->method2();

The nice thing about the static methods is that you can run some pre construction operations in the init methods such as constructor argument validation.



来源:https://stackoverflow.com/questions/11884855/creating-a-dynamic-class-instance-programmatically-in-php-with-variable-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!