__Call magic creates new classes in PHP

I use __call magic in some of my mvc code to create a framework for startup forms, but I have a problem in which I hope someone here can have a job.

__Call magic takes two parameters: $ methodName and $ arguments. The arguments are returned as an array of arguments that you named. Usually this function is called by methods that you can do as such to pass these arguments to the function:

call_user_func_array($methodName, $arguments);

And this will propogate the signature methods with arguments. I am trying to do something more complex. I am trying to distribute the class constructor in the same way, being able to send a possibly blurred comma separator array of arguments to the class constructor or just send the arguments directly, but both of them do not give the desired result. When I send the mounted array to the constructor, PHP thinks it is a string, and when I send the array, it thinks it is an array.

This is the code I'm using atm:

public function __call($method, $args){

    return $this->init_element(new $method($args['name'], $args['value'], $args['opts']));

}

What if I had 4 arguments to go through? Is there a way to make it dynamically populate the constructor in the same way you can for a function using call_user_func_array ()?

I could use the attributes () function for this, but I really would like to do it as I can with the help of the functions.

Thanks in advance,

+3
2

PHP: (http://php.net/manual/en/class.reflectionclass.php)

$obj = new ReflectionClass( $classname );

$ins = $obj->newInstanceArgs( $arguments );

$ins = $obj->newInstance( );

0

- :

 call_user_func_array(array($obj, '__construct'), $args);
-1

Source: https://habr.com/ru/post/1763593/


All Articles