Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
The Case of the Null Reference: Code Listing 3
class NaturalNumber {
private $n;
public function __construct(int &$n) {
if ($n < 1) {
throw new InvalidArgumentException('n must be greater than zero, got ' . $n);
$this->n =& $n;
public function toInt(): int {
return $this->n;
$n = 42;
$nat = new NaturalNumber($n);
$n = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment