News新闻

业界新闻动态、技术前沿
Who are we?

您的位置:首页      乐道系统FAQ      PHP中串行化用法示例

PHP中串行化用法示例

发布日期:2016-11-16 00:00:00 46

本文实例讲述了PHP中串行化用法。分享给大家供大家参考,具体如下:

功能:串行化用于对对象的存储或者传输,通过反串行化得到这个对象。

1. Person.class.php:

<?php
/*
作者 : shyhero
*/
class Person{ //声明一个Person类
  public $age;
  private $name;
  protected $sex;
  public function __construct($age="",$name="",$sex=""){
   $this -> age = $age;
   $this -> name = $name;
   $this -> sex = $sex;
  }
  public function say(){
   return $this -> age." ".$this -> name." ".$this -> sex;
  }
  function __sleep(){ //指定串行化时能提取的成员属性,没有参数,但是必须返回一个数组
   $arr = array("age","name");
   return $arr;
  }
  function __wakeup(){ //指定反串行化时,提取出来的值
   $this -> sex = "woman";
  }
}