简体   繁体   English

如何将变量分配给对象,而不是对其的引用?

[英]How do I assign a variable to an object, not a reference to it?

$a = some object;
$b = another object;
function do_some_stuff() {
    $c = $a;
    $b->a_property = $c;
}
do_some_stuff();
echo $b->a_property; # Undefined, becuase $c was deleted when the function exited.

When I type $b->a_property = $c , I want the PHP interpereter to do $b->a_property = $a . 当我输入$b->a_property = $c ,我希望PHP交易者执行$b->a_property = $a How do I do that, if I'm unable to assign it to $a directly? 如果无法直接将其分配给$a怎么办?

My actual code is: 我的实际代码是:

function setup() {
    $playerHeroes = [];
    $enemyHeroes = [];
    foreach ($entities as $object) {
        if ($object->team == "player") {
            $playerHeroes[] = $object;
        }
        else if ($object->team == "enemy") {
            $enemyHeroes[] = $object;
        }
    }

    foreach ($playerHeroes as $object) {
        $object->target = $enemyHeroes[array_rand($enemyHeroes)];
    }
    foreach ($enemyHeroes as $object) {
        $object->target = $playerHeroes[array_rand($playerHeroes)];
    }
}

I sort through a list of entities in the game as either being on the player's team or being on the enemy's team. 我对游戏中的实体列表进行了排序,要么是属于玩家团队,要么是敌方团队。 Each hero must target a hero on the opposing team. 每个英雄都必须将对方英雄作为目标。 When the setup function exits, $object , $playerHeroes and $enemyHeroes get destroyed. setup函数退出, $object$playerHeroes$enemyHeroes被摧毁。 The ->target properties are accessed later, and it's null . ->target属性将在以后访问,并且为null How do I make it so that when I assign a variable to a reference of an object, it assigns it to the object itself? 如何做到这一点,以便在将变量分配给对象的引用时将其分配给对象本身?

EDIT: I want the variable to change when the object changes, so cloning/copying by value is not an option. 编辑:我希望对象更改时更改变量,因此按值克隆/复制不是一种选择。

In addition to echoing the comments from @IvoP and @mega6382, it's worth noting that the problem you're having is not a "solvable" one per se. 除了回显@IvoP和@ mega6382的注释之外,值得注意的是,您所遇到的问题本身并不是一个“可解决的”问题。 That's because what you're running into is fundamental to how objects work in PHP. 这是因为您遇到的问题对于PHP中对象的工作方式至关重要。 You can learn all about it at http://ca2.php.net/manual/en/language.oop5.references.php . 您可以在http://ca2.php.net/manual/en/language.oop5.references.php上了解有关它的全部信息。

One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". 经常提到的PHP 5 OOP的关键点之一是“默认情况下,对象是通过引用传递的”。 This is not completely true. 这不是完全正确的。 This section rectifies that general thought using some examples. 本节使用一些示例来纠正一般性想法。

A PHP reference is an alias, which allows two different variables to write to the same value. PHP引用是一个别名,它允许两个不同的变量写入相同的值。 As of PHP 5, an object variable doesn't contain the object itself as value anymore. 从PHP 5开始,对象变量不再包含对象本身作为值。 It only contains an object identifier which allows object accessors to find the actual object. 它仅包含一个对象标识符,该标识符使对象访问者可以找到实际的对象。 When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object. 当对象通过参数发送,返回或分配给另一个变量时,不同的变量不是别名:它们持有标识符的副本,该副本指向同一对象。 (hat tip to @tomzx) (@tomzx的提示)

You'll likely do better to rearchitect your code to deal with this or use a language whose default behavior is call-by-value like C or Java. 您最好重新整理代码以解决此问题,或者使用默认行为是按值调用的语言(例如C或Java)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM