<bdo id='cVd0R'></bdo><ul id='cVd0R'></ul>

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

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

      在类定义上的构造函数 VS 上设置变量

      时间:2023-06-22
        <legend id='W95PX'><style id='W95PX'><dir id='W95PX'><q id='W95PX'></q></dir></style></legend>

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

        • <small id='W95PX'></small><noframes id='W95PX'>

          <tfoot id='W95PX'></tfoot>

              <bdo id='W95PX'></bdo><ul id='W95PX'></ul>

                本文介绍了在类定义上的构造函数 VS 上设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                最近我一直在想,在类定义上的构造函数 VS 上初始化具有默认值的变量是否有区别.

                Lately I've been wondering if there's a difference between initializing the variables that have a default value on the Constructor VS on the class definition.

                考虑到优化,哪个更好:

                Which one is better, taking into consideration the optimization:

                class TestClass
                {
                 private $test_var = 'Default Value';
                 function __construct()
                 {
                 }
                }
                
                class TestClass2
                {
                 private $test_var;
                 function __construct()
                 {
                  $this->test_var = 'Default Value';
                 }
                }
                

                推荐答案

                在构造函数之外初始化属性的好处是,有人阅读你的代码会立即知道它的默认值.

                The advantage of initializing properties outside of the constructor is that someone reading your code will immediatly know its a default value.

                不便之处在于您不能以这种方式使用所有类型的数据——例如,不适用于对象实例化,或者我记得的 heredoc 语法.

                Inconvenient is you cannot use every kind of data this way -- will not work with object instanciations, for instance, or with heredoc syntax, from what I recall.


                我认为在性能方面没有太大区别 - 无论如何,在您的应用程序中可能有很多更重要的事情;-)


                I don't think there is much of a difference when it comes to performance -- anyway, there are probably lots of things that matter a lot more, in your application ;-)


                不过,纯粹是为了好玩,使用 Vulcan Logic Disassembler :


                Still, purely for fun, using the Vulcan Logic Disassembler :

                以第一个代码示例(temp-2.php):

                With the first example of code (temp-2.php) :

                <?php
                class TestClass {
                    private $test_var = 'Default Value';
                    function __construct() {
                    }
                }
                $a = new TestClass();
                

                你得到这些操作码:

                $ php -d extension=vld.so -d vld.active=1 temp-2.php
                Branch analysis from position: 0
                Return found
                filename:       /home/squale/developpement/tests/temp/temp-2.php
                function name:  (null)
                number of ops:  11
                compiled vars:  !0 = $a
                line     #  op                           fetch          ext  return  operands
                -------------------------------------------------------------------------------
                   2     0  EXT_STMT
                         1  NOP
                   7     2  EXT_STMT
                         3  ZEND_FETCH_CLASS                                 :1      'TestClass'
                         4  EXT_FCALL_BEGIN
                         5  NEW                                              $2      :1
                         6  DO_FCALL_BY_NAME                              0
                         7  EXT_FCALL_END
                         8  ASSIGN                                                   !0, $2
                         9  RETURN                                                   1
                        10* ZEND_HANDLE_EXCEPTION
                
                Class TestClass:
                Function __construct:
                Branch analysis from position: 0
                Return found
                filename:       /home/squale/developpement/tests/temp/temp-2.php
                function name:  __construct
                number of ops:  4
                compiled vars:  none
                line     #  op                           fetch          ext  return  operands
                -------------------------------------------------------------------------------
                   4     0  EXT_NOP
                   5     1  EXT_STMT
                         2  RETURN                                                   null
                         3* ZEND_HANDLE_EXCEPTION
                
                End of function __construct.
                
                End of class TestClass.
                

                虽然,使用第二个代码示例(temp-3.php):

                While, with the second example of code (temp-3.php) :

                <?php
                class TestClass2 {
                    private $test_var;
                    function __construct() {
                        $this->test_var = 'Default Value';
                    }
                }
                $a = new TestClass2();
                

                你得到这些操作码:

                $ php -d extension=vld.so -d vld.active=1 temp-3.php
                Branch analysis from position: 0
                Return found
                filename:       /home/squale/developpement/tests/temp/temp-3.php
                function name:  (null)
                number of ops:  11
                compiled vars:  !0 = $a
                line     #  op                           fetch          ext  return  operands
                -------------------------------------------------------------------------------
                   2     0  EXT_STMT
                         1  NOP
                   8     2  EXT_STMT
                         3  ZEND_FETCH_CLASS                                 :1      'TestClass2'
                         4  EXT_FCALL_BEGIN
                         5  NEW                                              $2      :1
                         6  DO_FCALL_BY_NAME                              0
                         7  EXT_FCALL_END
                         8  ASSIGN                                                   !0, $2
                   9     9  RETURN                                                   1
                        10* ZEND_HANDLE_EXCEPTION
                
                Class TestClass2:
                Function __construct:
                Branch analysis from position: 0
                Return found
                filename:       /home/squale/developpement/tests/temp/temp-3.php
                function name:  __construct
                number of ops:  7
                compiled vars:  none
                line     #  op                           fetch          ext  return  operands
                -------------------------------------------------------------------------------
                   4     0  EXT_NOP
                   5     1  EXT_STMT
                         2  ZEND_ASSIGN_OBJ                                          'test_var'
                         3  ZEND_OP_DATA                                             'Default+Value'
                   6     4  EXT_STMT
                         5  RETURN                                                   null
                         6* ZEND_HANDLE_EXCEPTION
                
                End of function __construct.
                
                End of class TestClass2.
                

                所以,我猜有一点不同......但不是那么重要^^

                So, I'm guessing there is a bit of a difference... But not that important ^^

                由你来解释操作码——但有趣的是在第一次转储中没有默认值"的痕迹......有趣,实际上^^
                似乎 VLD 不能(或只是不能)转储所有内容:-(

                Up to you to interpret the opcodes -- but the funny thing is there is no trace of 'Default Value' in the first dump... interesting, actually ^^
                Seems VLD cannot (or just doesn't) dump everything :-(

                这篇关于在类定义上的构造函数 VS 上设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:PHP 构造函数的目的 下一篇:为什么我不能在 PHP 中重载构造函数?

                相关文章

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

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