Last Updated: September 09, 2019
·
677
· miguelmramos

PHP SPL_Types

When using experimental extension spl_types for php, as it is experimental documentation is very poor. If you need an Enumerator then make sure on your class that you insert the _constructor method and strict to true.

class EncoderLevelEnum extends \SplEnum {

const __default = self::FLOOR;

const FLOOR = '1A';
const CEIL = '2B';
const ROUND = '3C';

public function __construct($initial_value, $strict = true)
{
    parent::__construct($initial_value, $strict);
}
}

$enum = new EncoderLevelEnum(EncoderLevelEnum::FLOOR);
$fail = new EncoderLevelEnum(1,false);

What will happen if you put the incorrect $value is UnexpectedValueException isn't thrown correctly. What i have tested is if strict is false and you passe values from 1 to 3 class never throw the exception.