Late answer, but ...
According to PHP documentation , resource type vars cannot be serialized. However, at least in PHP 5.4, trying to serialize a resource does not cause any errors.
I think the best approach would be to test for closure and resources without try / catch:
$resource = fopen('composer.json', 'r'); $closure = function() { return 'bla'; }; $string = 'string'; function isSerializable($var) { if (is_resource($var)) { return false; } else if ($var instanceof Closure) { return false; } else { return true; } } var_dump(isSerializable($resource)); var_dump(isSerializable($closure)); var_dump(isSerializable($string));
Outputs:
boolean false boolean false boolean true
source share