3v4l.org

run code in 300+ PHP versions simultaneously
<?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());

preferences:
26.9 ms | 404 KiB | 5 Q