<?php
class Reference {
private static $DATA = [];
private $index = 0;
public function __construct($value) {
// cannot create a reference of a reference
if ($value instanceof self) {
$this->index = $value->index;
} else {
$this->index = count(self::$DATA);
self::$DATA[$this->index] = $value;
}
}
public function get() {
return self::$DATA[$this->index];
}
public function set($value) {
self::$DATA[$this->index] = $value;
}
}
$orig = [1];
// $ref =& $orig[0];
$orig[0] = new Reference($orig[0]);
$ref = clone $orig[0];
$copy = $orig;
$copy[0] = clone $orig[0]; // creating a copy of what was in $orig[0]
// $orig[0] = 10;
$orig[0]->set(10);
// var_dump($copy[0]);
var_dump($copy[0]->get());
- Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- int(10)
preferences:
171.89 ms | 404 KiB | 215 Q