xiaoyantongxue
<?php
//数组的序列化
/*
$stu=[\'tom\',\'berry\',\'ketty\'];
$str=serialize($stu);        //序列化
file_put_contents(\'./stu.txt\',$str);
*/

//数组的反序列化
$str=file_get_contents(\'./stu.txt\');
$stu=unserialize($str);        //反序列化
print_r($stu);    //Array ( [0] => tom [1] => berry [2] => ketty ) 

 

#### 1.5.2  对象的序列化与反序列化

注意:对象的反序列化需要有类的参与,如果没有类在反序列化时候无法确定类



<?php
class Student {
    public $name;
    protected $sex;
    private $add;
    public function __construct($name,$sex,$add) {
        $this->name=$name;
        $this->sex=$sex;
        $this->add=$add;
    }
}
/*
//测试
$stu=new Student(\'tom\',\'男\',\'北京\');
//序列化
$str=serialize($stu);
file_put_contents(\'./stu.txt\',$str);
*/

//反序列化,类的反序列化必须要有类的参与
$str=file_get_contents(\'./stu.txt\');
$stu=unserialize($str);
echo \'<pre>\';
var_dump($stu);

 

分类:

技术点:

相关文章:

  • 2021-06-16
  • 2021-10-18
  • 2022-01-17
  • 2022-02-19
  • 2022-12-23
  • 2021-11-25
  • 2021-10-03
猜你喜欢
  • 2021-10-05
  • 2021-09-07
  • 2021-11-16
  • 2022-01-20
  • 2021-08-22
  • 2022-12-23
  • 2022-01-28
相关资源
相似解决方案