开发者

Test if a variable is serializable

I'm looking for an elegant way of testing if a variable is serializable. For example array( function() {} ) will fail to serialize.

I'm currently using the code below, but it seems to be a rather non-optimal way of doing it.

function isSerializable( $var )
{
    try {
        serialize( $var );
        return TRUE;
    } catch( Exception $e ) {
        return FALSE;
    }
}

var_dump( isSerializable( array() ) );                // bool(true)
var_dump( isSerializable( function() {} ) );          // bool(false)
var_dump( isSerializable( array( f开发者_开发技巧unction() {} ) ) ); // bool(false)


The alternative could be:

function isSerializable ($value) {
  $return = true;
  $arr = array($value);

  array_walk_recursive($arr, function ($element) use (&$return) {
    if (is_object($element) && get_class($element) == 'Closure') {
      $return = false;
    }
  });

  return $return;
}

But from comments I think this is what you are looking for:

function mySerialize ($value) {
  $arr = array($value);

  array_walk_recursive($arr, function (&$element) {

    # do some special stuff (serialize closure) ...
    if (is_object($element) && get_class($element) == 'Closure') {
      $serializableClosure = new SerializableClosure($element);
      $element = $serializableClosure->serialize();
    }

  });

  return serialize($arr[0]);
}


Late answer but...

According to PHP documentation, vars of the Resource type cannot be serialized. However, at least in PHP 5.4, trying to serialize a Resource will not trigger any error.

I think a better approach would be to test for closures 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


This is what I'm using. It combines a number of suggestions here, plus a check for instaneof ArrayAccess which should be serializable.

if (!function_exists('is_iterable')) {
    function is_iterable($var) {
        return is_array($var) || (is_object($var) && ($var instanceof \Traversable));
    }
}

function is_serializable($var, $iterate=true) {
    if (is_resource($var)) {
        return false;
    } else if (is_object($var)) {
        if ($var instanceof Closure) {
            return false;
        } else if (!$var instanceof Serializable && !$var instanceof ArrayAccess) {
            return false;
        }
    }

    if ($iterate && is_iterable($var)) {
        foreach ($var as $key => $value) {
            if (!is_serializable($value, true)) {
                return false;
            }
        }
    }

    return true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜