The reason why you don't get an expected value after unserialize is that SplEnum is not just a regualar PHP class. It is a kind of hack. The value is kept in its own structure, not in PHP object itself as you can see here
https://github.com/davidcoallier/SPL_Types/blob/master/spl_type.c in method spl_type_object_new_ex
They create an internal structure "object"
object = emalloc(sizeof(spl_type_object));
that holds the value
ZVAL_ZVAL(object->value, *def, 1, 0);
The unserialize
function does not know a thing about this internal object and cannot change its value. Hence this peculiar behaviour.
Update
You might want to use casting in order to preserve the object's value. In your case it could have been:
$casted_value = (int) $t;
At least an integer is easier to handle.
Update 2
A working example with working serialization.
class Test2 extends SplEnum implements Serializable
{
const __default = 1;
const A = 1;
const B = 27;
const C = 31;
public function serialize() {
return serialize ((int) $this);
}
public function unserialize($serialized) {
$this->__construct (unserialize($serialized));
}
}