我在PHP中派生SplEnum
类使我的类像
我试图通过制作构造函数final
并在内部抛出异常来阻止实例化。
这是我的代码片段。
final class UserType extends \SplEnum{
const __default = self::UNKNOWN;
const UNKNOWN = 0;
const STUDENT = 1;
const STARTUP = 2;
const JURY = 3;
const MENTOR = 4;
const ADMIN = 5;
const SUPERADMIN = 6;
// Here's my thought on how the instantiation could be prevented
public final function __construct(){
throw new Exception('This class is supposed to be Enum, cannot instantiate this class');
}
public static function isValidUserTypeKey($name,$strict = false){}
public static function isValidUserTypeValue($value) {}
}
这是正确的方式还是可以有更好的实现方式?
感谢。