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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在构造函数VS上设置变量在类定义上脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
最近我一直在想,是否类定义上初始化构造函数VS上的认值的变量之间有区别.

一个更好,考虑到优化:

class testClass
{
 PRivate $test_VAR = 'Default Value';
 function __construct()
 {
 }
}

class TestClass2
{
 private $test_var;
 function __construct()
 {
  $this->test_var = 'Default Value';
 }
}
初始化构造函数之外的属性的优点是,读取代码的人将立即知道认值.

不方便的是,您不能以这种方式使用各种数据 – 不会使用对象实例,例如或使用heredoc语法,从我记得.

我认为在性能方面有很大的不同 – 反正在你的应用程序中,可能还有很多事情会变得更重要;-)

仍然,纯粹为了乐趣,使用Vulcan LOGic DisasSEMbler:

使用第一个代码示例(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):

<?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.

所以,我猜想有一点区别…但不是那么重要^^

直到你解释操作码 – 但有趣的是,在第一个转储中没有“认值”的痕迹…有趣,实际上是^^似乎VLD不能(或只是不)倾倒一切:-(

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在构造函数VS上设置变量在类定义上全部内容,希望文章能够帮你解决php – 在构造函数VS上设置变量在类定义上所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。