Skip to content

Instantly share code, notes, and snippets.

@Fleshgrinder
Created February 19, 2017 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fleshgrinder/366e33ab4b42aaa87fa0656385b77248 to your computer and use it in GitHub Desktop.
Save Fleshgrinder/366e33ab4b42aaa87fa0656385b77248 to your computer and use it in GitHub Desktop.
The Case of the Null Reference: Code Listing 3
<?php
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;
$nat->toInt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment