- var_dump: documentation ( source)
<?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());