Dynamically call Class with variable number of parameters in the constructor

后端 未结 4 1204
广开言路
广开言路 2020-12-09 10:35

I know that it is possible to call a function with a variable number of parameters with call_user_func_array() found here -> http://php.net/manual/en/function.call-user-func

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 10:40

    I've found here

    Is there a call_user_func() equivalent to create a new class instance?

    the example:

    function createInstance($className, array $arguments = array())
    {
        if(class_exists($className)) {
            return call_user_func_array(array(
                new ReflectionClass($className), 'newInstance'), 
                $arguments);
        }
        return false;
    }
    

    But can somebody tell me if there is an example for classes with protected constructors?

提交回复
热议问题