Using class constants in PHP, is it possible to enforce that an argument must be one of the constants (like an Enum)?
Class Server {
const SMALL = "s";
const MEDIUM = "m";
const LARGE = "l";
private $size;
public function __construct($size) {
$this->size = $size
}
}
$small = new Server(Server::SMALL); // valid
$foobar = new Server("foobar"); // also valid. This should fail instead
The case of new Server("foobar")
should fail, but because the constructor does not validate $size
this works. For brevity I only listed three constant sizes, but assume there are N number of sizes so don't want to do a big block of if checks.