<bdo id='Pw8dX'></bdo><ul id='Pw8dX'></ul>
<legend id='Pw8dX'><style id='Pw8dX'><dir id='Pw8dX'><q id='Pw8dX'></q></dir></style></legend>
  1. <i id='Pw8dX'><tr id='Pw8dX'><dt id='Pw8dX'><q id='Pw8dX'><span id='Pw8dX'><b id='Pw8dX'><form id='Pw8dX'><ins id='Pw8dX'></ins><ul id='Pw8dX'></ul><sub id='Pw8dX'></sub></form><legend id='Pw8dX'></legend><bdo id='Pw8dX'><pre id='Pw8dX'><center id='Pw8dX'></center></pre></bdo></b><th id='Pw8dX'></th></span></q></dt></tr></i><div id='Pw8dX'><tfoot id='Pw8dX'></tfoot><dl id='Pw8dX'><fieldset id='Pw8dX'></fieldset></dl></div>

      <tfoot id='Pw8dX'></tfoot>
    1. <small id='Pw8dX'></small><noframes id='Pw8dX'>

    2. 如何在 PHP 中使用 call_user_func_array 调用构造函数

      How to call the constructor with call_user_func_array in PHP(如何在 PHP 中使用 call_user_func_array 调用构造函数)
      <legend id='7jVTh'><style id='7jVTh'><dir id='7jVTh'><q id='7jVTh'></q></dir></style></legend>

          <tbody id='7jVTh'></tbody>
            • <bdo id='7jVTh'></bdo><ul id='7jVTh'></ul>

              • <tfoot id='7jVTh'></tfoot>

                <small id='7jVTh'></small><noframes id='7jVTh'>

                <i id='7jVTh'><tr id='7jVTh'><dt id='7jVTh'><q id='7jVTh'><span id='7jVTh'><b id='7jVTh'><form id='7jVTh'><ins id='7jVTh'></ins><ul id='7jVTh'></ul><sub id='7jVTh'></sub></form><legend id='7jVTh'></legend><bdo id='7jVTh'><pre id='7jVTh'><center id='7jVTh'></center></pre></bdo></b><th id='7jVTh'></th></span></q></dt></tr></i><div id='7jVTh'><tfoot id='7jVTh'></tfoot><dl id='7jVTh'><fieldset id='7jVTh'></fieldset></dl></div>
                本文介绍了如何在 PHP 中使用 call_user_func_array 调用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何使用 call_user_func_array 调用类的构造函数

                How could I call the constructor of a class with call_user_func_array

                这是不可能的:

                $obj = new $class();
                call_user_func_array(array($obj, '__construct'), $args); 
                

                因为如果构造函数有参数,new 就会失败.

                because if the constructor has parameters, the new will fail.

                约束:我不能控制必须实例化的类,也不能修改它们.

                Constraint : I do not control the classes that I have to instantiate, nor can I modify them.

                别问我为什么要做这个疯狂的事情,这是一个疯狂的测试.

                Don't ask me why I want to do this crazy thing, this is a crazy test.

                推荐答案

                你可以使用 reflection 像:>

                You can use reflection like:

                $reflect  = new ReflectionClass($class);
                $instance = $reflect->newInstanceArgs($args);
                

                <小时>

                从 PHP 5.6.0 开始,... 运算符也可用于此目的.


                As of PHP 5.6.0, the ... operator can also be used for this purpose.

                $instance = new $class(...$args);
                

                <小时>

                if(version_compare(PHP_VERSION, '5.6.0', '>=')){
                    $instance = new $class(...$args);
                } else {
                    $reflect  = new ReflectionClass($class);
                    $instance = $reflect->newInstanceArgs($args);
                }
                

                这篇关于如何在 PHP 中使用 call_user_func_array 调用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How do I pass parameters into a PHP script through a webpage?(如何通过网页将参数传递给 PHP 脚本?)
                PHP - include a php file and also send query parameters(PHP - 包含一个 php 文件并发送查询参数)
                Where can I read about conditionals done with quot;?quot; and quot;:quot; (colon)?(我在哪里可以阅读有关使用“?完成的条件的信息?和“:(冒号)?)
                Accessing arrays whitout quoting the key(在不引用键的情况下访问数组)
                What is the name for the quot;lt;lt;lt;quot; operator?(“lt;lt;lt;的名字是什么?操作员?)
                default as first option in switch statement?(默认为 switch 语句中的第一个选项?)

                1. <small id='Zo7BY'></small><noframes id='Zo7BY'>

                2. <legend id='Zo7BY'><style id='Zo7BY'><dir id='Zo7BY'><q id='Zo7BY'></q></dir></style></legend>

                    <tbody id='Zo7BY'></tbody>
                        <bdo id='Zo7BY'></bdo><ul id='Zo7BY'></ul>

                      • <tfoot id='Zo7BY'></tfoot>
                        • <i id='Zo7BY'><tr id='Zo7BY'><dt id='Zo7BY'><q id='Zo7BY'><span id='Zo7BY'><b id='Zo7BY'><form id='Zo7BY'><ins id='Zo7BY'></ins><ul id='Zo7BY'></ul><sub id='Zo7BY'></sub></form><legend id='Zo7BY'></legend><bdo id='Zo7BY'><pre id='Zo7BY'><center id='Zo7BY'></center></pre></bdo></b><th id='Zo7BY'></th></span></q></dt></tr></i><div id='Zo7BY'><tfoot id='Zo7BY'></tfoot><dl id='Zo7BY'><fieldset id='Zo7BY'></fieldset></dl></div>