带括号的实例化与不带括号的实例化之间有区别吗? [英] Is there a difference between instantiation with parentheses or without?

查看:89
本文介绍了带括号的实例化与不带括号的实例化之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两段代码有什么区别?

What's the difference between these 2 piece of codes?

<?php

    $object1 = new User();
                     //^^
    $object1->name = "Hello";        
    echo $object1->name;
    class User {}

?>

并且:

<?php

    $object1 = new User;
                    //^
    $object1->name = "Hello";        
    echo $object1->name;
    class User {}

?>

我得到相同的输出:

Hello

因此,如果我不使用括号,有什么区别:

So is there any difference if I use the parentheses or not in:

$object1=new User;

推荐答案

完全相同,您可以比较以下两个脚本的操作码:

The are exactly the same, you can compare opcode of these 2 scripts:

1个脚本:

$object1=new User();
$object1->name="Hello";        
echo $object1->name;
class User {}

操作码:

line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   3     0  >   FETCH_CLASS                                   4  :0      'User'
         1      NEW                                              $1      :0
         2      DO_FCALL_BY_NAME                              0          
         3      ASSIGN                                                   !0, $1
   4     4      ASSIGN_OBJ                                               !0, 'name'
         5      OP_DATA                                                  'Hello'
   5     6      FETCH_OBJ_R                                      $5      !0, 'name'
         7      ECHO                                                     $5
   6     8      NOP                                                      
         9    > RETURN                                                   1

2个脚本:

$object1=new User;
$object1->name="Hello";        
echo $object1->name;
class User {}

操作码:

line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   3     0  >   FETCH_CLASS                                   4  :0      'User'
         1      NEW                                              $1      :0
         2      DO_FCALL_BY_NAME                              0          
         3      ASSIGN                                                   !0, $1
   4     4      ASSIGN_OBJ                                               !0, 'name'
         5      OP_DATA                                                  'Hello'
   5     6      FETCH_OBJ_R                                      $5      !0, 'name'
         7      ECHO                                                     $5
   6     8      NOP                                                      
         9    > RETURN                                                   1

这篇关于带括号的实例化与不带括号的实例化之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆