【问题标题】:Copy of object identifier and reference to object identifier - which one should be used in real app?对象标识符的副本和对对象标识符的引用 - 在实际应用程序中应该使用哪一个?
【发布时间】:2012-01-22 15:18:10
【问题描述】:

在下面的例子中,$instance2$instance3 所做的任何操作都会修改原始对象。

我的问题是:

如果原始对象标识符的副本和对原始对象标识符的引用执行相同的工作,那么在实际应用中应该使用哪一个?

使用对象标识符的副本和使用对象标识符的引用有什么优缺点?

我阅读了PHP manual,但无法区分用法,因为两者的工作相同。

$instance1 = new test(1);
$instance2 = $instance1;
$instance3 =& $instance1;

//$instance1 -> original object identifier of the new object.
//$instance2 -> copy of object identifier $instance1
//$instance3 -> reference to the object identifier $instance1

【问题讨论】:

    标签: php oop


    【解决方案1】:

    $instance2 有一个标识符副本到对象测试。因此,它包含与$instance1 相同的内容。 $instance3 包含对 $instance1 的引用。区别如下:

    $instance1 = new Test();
    $instance2 = $instance1;
    $instance3 = & $instance1;
    
    var_dump($instance1 instanceof Test); // True
    var_dump($instance2 instanceof Test); // True
    var_dump($instance3 instanceof Test); // True
    
    $instance3 = new AnotherTest();
    
    var_dump($instance1 instanceof AnotherTest); // True
    var_dump($instance2 instanceof AnotherTest); // False
    var_dump($instance3 instanceof AnotherTest); // True
    

    如果更改$instance1 而不是$instance3,将返回相同的输出。

    但如果我们执行以下操作:

    $instance1 = new Test();
    $instance2 = $instance1;
    $instance3 = & $instance1;
    
    $instance2 = new AnotherTest();
    
    var_dump($instance1 instanceof AnotherTest); // False
    var_dump($instance2 instanceof AnotherTest); // True
    var_dump($instance3 instanceof AnotherTest); // False
    

    所以:

    修改通过引用传递或通过引用分配的变量(使用& 操作数)或它所引用的变量,修改两者,而修改复制的变量只修改给定的变量。

    不过,您必须记住 $instance1 保留的是对象的标识符,所以:

    $instance1 = new StdClass();
    $instance2 = $instance1;
    
    $instance1->my_property = 1;
    var_dump($instance2->my_property); // Output: 1
    

    希望现在更清楚了。

    【讨论】:

    • 你的例子很有道理,但你不应该在 PHP 中使用“指针”字。
    • 我使用了“poitner”这个词,因为它们之间的主要区别正是引擎如何处理它们......我不知道如何用不同的方式表达它。
    • 正确的词是对象标识符:php.net/manual/en/language.oop5.references.php
    • 本文包含一些重要信息,说明使用引用与使用复制的对象标识符对性能的负面影响,因为 PHP 的运行时工作方式。一般来说,文章建议不要使用引用。 schlueters.de/blog/archives/125-Do-not-use-PHP-references.html
    【解决方案2】:

    在 PHP5 中使用对象,而不是整个对象,只复制对象标识符。这使得您不再需要引用相同的值,而只需复制标识符即可引用相同的对象。将其用于您的优势;你可以去掉 & 符号。

    一个重要的区别是分配给变量引用会影响原始变量,而分配给复制的对象标识符则不会。只有对对象的修改才会。如果您想以直观的方式使用 OO,这将是使用对象引用的原因。示例:

    $a = new stdClass();
    $a->name = "A";
    
    $b = new stdClass();
    $b->name = "B";
    
    // $x is a copy of the object identifier in $a
    $x = $a;
    // $y is a reference to $a
    $y = &$a;
    
    // this will not affect $a
    $x = $b;
    echo $a->name; // 'A'
    
    // this will affect $a
    $y = $b;
    echo $a->name; // 'B'
    

    另请参阅这篇文章,了解为什么应避免使用(与号)引用:

    http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html

    【讨论】:

    • 你能指出它们之间的任何显着差异吗?不使用 & 只是语法上的好处。否则两者都有相同的目的。
    • 我添加了一个示例,说明为什么利用对象标识符和避免使用变量引用 i.c.w 是一件好事。对象。
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    相关资源
    最近更新 更多