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.
Consider using the myclabs/php-enum package, which provides the functionality you are looking for.
If you try to pass an invalid enum value, an exception will be thrown.
Package docs: https://github.com/myclabs/php-enum#documentation
If your code is as simple as the sample, then it is very easy to implement a check that will do what you want. For example:
class Server {
const SMALL = "s";
const MEDIUM = "m";
const LARGE = "l";
private $size;
public function __construct($size) {
switch($size) {
case self::SMALL:
case self::MEDIUM:
case self::LARGE:
$this->size = $size;
break;
default:
throw new Exception('Invalid size passed.');
}
}
}
$S = new Server("s");
$Fail = new Server("foo");
And the output from above would be:
Fatal error: Uncaught Exception: Invalid size passed. in /devsites/tigger/t.php:18 Stack trace: #0 /devsites/tigger/t.php(24): Server->__construct('foo') #1 {main} thrown in /devsites/tigger/t.php on line 18