>  기사  >  php教程  >  php序列化serialize() 与反序列化unserialize()

php序列化serialize() 与反序列化unserialize()

WBOY
WBOY원래의
2016-05-23 08:33:562107검색

在php中,可以对数组,变量,对象等进行序列化(静态变量,常量不会被序列化)

<?php
    class TestSerialize{
        private $name;
        private $password;
        const ID = 1234;
        public static $_class_name = __CLASS__;
 
        public function __set($varname,$value){
 
            $this->$varname = $value;
        }
 
        public function __get($varname)
        {
            return $this->$varname;
        }
    }
 
    $data = array(
            &#39;name&#39;=>&#39;zhangsan&#39;,
            &#39;password&#39;=>&#39;pwd123&#39;
        );
     
     
    $test = &#39;abcd&#39;;
    $ts = new TestSerialize();
    $ts->name = &#39;zhangsan_2&#39;;
    $ts->password = &#39;pwd123_2&#39;;
    echo  &#39;var=>&#39;.serialize($test).&#39;<br/>&#39;;
    echo  &#39;array=>&#39;.serialize($data).&#39;<br/>&#39;;
    echo  &#39;object=>&#39;.serialize($ts).&#39;<br/>&#39;;
 
?>

输出结果

var=>s:4:"abcd";
array=>a:2:{s:4:"name";s:8:"zhangsan";s:8:"password";s:6:"pwd123";}
object=>O:13:"TestSerialize":2:{s:19:"TestSerializename";s:10:"zhangsan_2";s:23:"TestSerializepassword";s:8:"pwd123_2";}

序列化产生的数据可以保存在本地文件中,也可以进行网络传输

function writeObject($filename,$data=&#39;&#39;)
{
    if(!file_exists(dirname(__FILE__).&#39;/tmp/&#39;))
    {
        mkdir(dirname(__FILE__).&#39;/tmp/&#39;);
    }
    $fp = fopen ( dirname(__FILE__).&#39;/tmp/&#39;.$filename, "w+" );
    if (flock($fp, LOCK_EX )) //给文件加锁
    {
        fwrite($fp, $data );
        flock( $fp, LOCK_UN ); //给文件解锁
    } else {
        echo &#39;Couldn\&#39;t lock the file !&#39;;
    }
    fclose ( $fp );
}
 
function readObject($filename)
{
    if(file_exists(dirname(__FILE__).&#39;/tmp/&#39;.$filename))
    {
        $fp = fopen ( dirname(__FILE__).&#39;/tmp/&#39;.$filename, "r" );
        stream_set_timeout($fp,2);
        $contents = fread($fp, filesize(dirname(__FILE__).&#39;/tmp/&#39;.$filename));
        fclose ($fp);
 
        return $contents;
    }
     
    return NULL;
}

测试一下

writeObject(&#39;data.txt&#39;,serialize($data));
writeObject(&#39;array.txt&#39;,serialize($test));
writeObject(&#39;object.txt&#39;,serialize($ts));
 
echo readObject(&#39;data.txt&#39;);
echo readObject(&#39;data.txt&#39;);
echo readObject(&#39;data.txt&#39;);

打印结果

a:2:{s:4:"name";s:8:"zhangsan";s:8:"password";s:6:"pwd123";}s:4:"abcd";O:13:"TestSerialize":2:{s:19:"TestSerializename";s:10:"zhangsan_2";s:23:"TestSerializepassword";s:8:"pwd123_2";}

为了保证持久化的有效性,我们可以对从数据库读取出来的文件反序列化

$f_data =  readObject(&#39;data.txt&#39;);
$f_array =  readObject(&#39;array.txt&#39;);
$f_object =  readObject(&#39;object.txt&#39;);
 
$d = unserialize($f_data);
 
$a = unserialize($f_array);
 
$u = unserialize($f_object);
 
print_r($d);
echo &#39;<br/>&#39;;
echo $a;
echo &#39;<br/>&#39;;
echo $u->name;

输出结果

Array ( [name] => zhangsan [password] => pwd123 ) 
abcd
zhangsan_2


本文链接:

收藏随意^^请保留教程地址.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.