Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中非序列化函数的实现_Php_Serialization_Deserialization - Fatal编程技术网

php中非序列化函数的实现

php中非序列化函数的实现,php,serialization,deserialization,Php,Serialization,Deserialization,最近,我被要求编写自己的PHP serialize()、unserialize()函数的实现。我的函数应该返回与原始PHP内部函数相同的结果。 我在serialize()函数方面取得了一些进展: <?php function serializator($input) { $args = count(func_get_args()); if ( $args > 1) { throw new Exception("Serializator

最近,我被要求编写自己的PHP serialize()、unserialize()函数的实现。我的函数应该返回与原始PHP内部函数相同的结果。 我在serialize()函数方面取得了一些进展:

<?php
    function serializator($input)
    {
        $args = count(func_get_args());
        if ( $args > 1) { throw new Exception("Serializator function requires exactly 1 parameter. {$args} given"); };
        switch ($input) :
            case is_array($input) :
                $length = count($input);
                $str = gettype($input)[0] . ':' . $length . ':{';
                foreach ($input as $key => $value):
                    if (is_array($value)) {
                        $str .= gettype($key)[0] . ':' ;
                        $str .= !is_int($key) ? strlen($key) . ':' . '"' . $key . '"' . ';' : $key . ';';
                        $str .= serializator($value);
                    }
                    else {
                        $str .= gettype($key)[0] . ':' ;
                        $str .= !is_int($key) ? strlen($key) . ':' . '"' . $key . '"' . ';' : $key . ';';
                        $str .= gettype($value)[0] . ':' ;
                        $str .= !is_int($value) ?  strlen($value) . ':' . '"' . $value . '"' . ";" : $value . ';';
                    }
                endforeach;
                $str .= '}';
                break;
            case is_object( $input ) :
                $str = ucfirst(gettype($input)[0]) . ':' . strlen(get_class($input)) . ':' . '"' . get_class($input) . '"';
                $str .= substr(serializator((array)$input), 1);
                break;
            case is_int( $input) || is_bool($input) || is_null($input) :
                $str = gettype($input)[0] . ':' . $input;
                break;
            case is_string( $input) :
                $str = gettype($input)[0] . ':' . strlen($input). ':' . '"' . $input . '"';
                break;
            case is_float($input) :
                $precision = ini_get('serialize_precision');
                $str = gettype($input)[0] . ':' . $input;
                break;
            case is_resource($input):
                $str = 'i:0'; // resource is not serializable
                break;
            default:
                return false;
        endswitch;

        return $str;
    }