Created
February 19, 2017 14:20
-
-
Save Fleshgrinder/366e33ab4b42aaa87fa0656385b77248 to your computer and use it in GitHub Desktop.
The Case of the Null Reference: Code Listing 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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