Skip to content

Instantly share code, notes, and snippets.

@newhope
Last active July 6, 2022 03:37
Show Gist options
  • Save newhope/a04cfa1b9b0674cfbdabfaba7166081c to your computer and use it in GitHub Desktop.
Save newhope/a04cfa1b9b0674cfbdabfaba7166081c to your computer and use it in GitHub Desktop.
PHP Pointer Class
<?php
class Pointer {
private $obj;
public function __construct( & $anything) {
$this->obj = & $anything;
}
public function & get() {
return $this->obj;
}
}
// Sample usage
$v1 = 'a';
$p1 = new Pointer($v1);
echo $p1->get(); // Output: 'a'
$v1 = 'b';
echo $p1->get(); // Output: 'b'
$same_val = & $p1->get();
$v1 = 'c';
echo $same_val; // Output: 'c'
$same_val = 'd';
echo $v1; // Output: 'd'
$p2 = new Pointer($same_val);
$v1 = 'e';
echo $p1->get(); // Output: 'e'
echo $p2->get(); // Output: 'e'
$same_val = 'f';
echo $p1->get(); // Output: 'f'
echo $p2->get(); // Output: 'f'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment