复制对象列表时不能松散引用

时间:2018-02-21 21:43:16

标签: php oop reference

我在这里有这个代码,无论我做什么,当我做var_dump()时,数组中的所有项都是相同的。我不知道为什么我不能放弃参考以及如何处理它。

这是一个复制相同行为的代码简化

class Numbr {
    public $value = NULL;

    function __construct(int $value) {
        $this->value = $value;
    }

    public function multiply(int $multiplier) {
        $this->value *= $multiplier;
    }
}

class Test {
    public $operations = [];

    function __construct($values) {
        $operations = [];
        foreach($values AS $value) $operations[] = new Numbr($value);
        $this->operations = $operations;
    }

    public function simulation() {
        $ops = $this->operations;

        //Here I make 4 copies of $ops that I want to end up being different
        $op1 = $this->transformation($ops, 2);
        $op2 = $this->transformation($ops, -2);
        $op3 = $this->transformation($ops, -1);
        $op4 = $this->transformation($ops, 4);

        $this->operations = [$op1, $op2, $op3, $op4];
    }

    public function transformation(array $ops, int $value) {
        $output = [];
        foreach($ops AS $N) {
            $N->multiply($value);
            $output[] = $N;
        }
        return $output;
    }
}

$Test = new Test([1,2,3,4]);
$Test->simulation();
var_dump($Test)

2 个答案:

答案 0 :(得分:0)

感谢@AbraCadaver这里是解决方案

public function transformation(array $ops, int $value) {
        $output = [];
        foreach($ops AS $N) {
            $P = clone($N); //But why?
            $P->multiply($value);
            $output[] = clone($P);
        }
        return $output;
    }

答案 1 :(得分:0)

var_dump的输出解释了。

object(Test)#1 (1) {
  ["operations"]=>
  array(4) {
    [0]=>
    array(4) {
      [0]=>
      object(Numbr)#2 (1) {
        ["value"]=>
        int(16)
      }
      [1]=>
      object(Numbr)#3 (1) {
        ["value"]=>
        int(32)
      }
      [2]=>
      object(Numbr)#4 (1) {
        ["value"]=>
        int(48)
      }
      [3]=>
      object(Numbr)#5 (1) {
        ["value"]=>
        int(64)
      }
    }
    [1]=>
    array(4) {
      [0]=>
      object(Numbr)#2 (1) {
        ["value"]=>
        int(16)
      }
      [1]=>
      object(Numbr)#3 (1) {
        ["value"]=>
        int(32)
      }
      [2]=>
      object(Numbr)#4 (1) {
        ["value"]=>
        int(48)
      }
      [3]=>
      object(Numbr)#5 (1) {
        ["value"]=>
        int(64)
      }
    }
    [2]=>
    array(4) {
      [0]=>
      object(Numbr)#2 (1) {
        ["value"]=>
        int(16)
      }
      [1]=>
      object(Numbr)#3 (1) {
        ["value"]=>
        int(32)
      }
      [2]=>
      object(Numbr)#4 (1) {
        ["value"]=>
        int(48)
      }
      [3]=>
      object(Numbr)#5 (1) {
        ["value"]=>
        int(64)
      }
    }
    [3]=>
    array(4) {
      [0]=>
      object(Numbr)#2 (1) {
        ["value"]=>
        int(16)
      }
      [1]=>
      object(Numbr)#3 (1) {
        ["value"]=>
        int(32)
      }
      [2]=>
      object(Numbr)#4 (1) {
        ["value"]=>
        int(48)
      }
      [3]=>
      object(Numbr)#5 (1) {
        ["value"]=>
        int(64)
      }
    }
  }
}

所有对象都已编号。每个数组包含相同的4个对象,[object(Numbr)#2object(Numbr)#3object(Numbr)#4object(Numbr)#5]

当你创建一个新数组时,你可能想要克隆这些对象而不是重用它们,或者只是$output[] = $N->value它只是用于输出。