unserialize(),serialize(),__wakeup(),__sleep()函数

基于序列化与反序列化,在此我只是举一个例子,For example:

class student{
    private $name;
    private $age;
    private $address;

    public function __construct($name,$age,$address){
        $this->name=$name;
        $this->age=$age;
        $this->address=$address;   
    }

    public function information(){
        echo "My name:".$this->name.";My age:".$this->age."My address:".$this->address;
    }
}

$P=new student("小明","25","湖北");
$P->information();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
output: My name:小明;My age:25;My address:湖北;
  • 1
  • 1

For example:

class student{
    private $name;
    private $age;
    private $address;

    public function __construct($name,$age,$address){
        $this->name=$name;
        $this->age=$age;
        $this->address=$address;   
    }

    public function __sleep(){
        $arr=array("name","age");
        return ($arr);
    }

    public function __walkup(){
        $this->age=30;
    }

    public function information(){
        echo "My name:".$this->name.";My age:".$this->age."My address:".$this->address;
    }
}

$P=new student("小红",30,"湖南");
$P->information();
echo "<br>";
$String=serialize($P);
echo $String;
echo "<br>";
$String2=unserialize($String);
$String2->information();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

output:

My name:小红;My age:30My address:湖南
O:7:"student":2:{s:13:"studentname";s:6:"小红";s:12:"studentage";i:30;}
My name:小红;My age:30My address:
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

参考资料:http://www.php-note.com/article/detail/35

posted @ 2016-12-22 22:17  天涯海角路  阅读(173)  评论(0)    收藏  举报