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( function() {} ) ) ); // bool(false)
Edit
Report