PHP中串行化用法
串行化大概就是把一些变量转化成为字符串的字节流的形式,这样比较容易传输、存储。当然,关是传输存储没有什么,关键是变成串的形式以后还能够转化回来,而且能够保持原来数据的结构。以下是小编为大家搜索整理的PHP中串行化用法,希望能给大家带来帮助!更多精彩内容请及时关注我们应届毕业生考试网!
1. Person.class.php:
*/
class Person{ /pic/p>
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(){ /pic/p>
$arr = array("age","name");
return $arr;
}
function __wakeup(){ /pic/p>
$this -> sex = "woman";
}
}
2. 串行化代码
require("./Person.class.php");
$p = new Person(21,"du","man"); /pic/p>
$pString = serialize($p); /pic/p>
file_put_contents("./file.txt",$pString);/pic/p>
3. 反串行化代码
require("./Person.class.php");/pic/p>
$pString = file_get_contents("./file.txt");/pic/p>
$p = unserialize($pString);/pic/p>
【PHP中串行化用法】相关文章:
php中fsockopen用法实例08-14
php中引用的用法分析03-04
php中rename函数用法11-27
php中return的用法实例分析09-04
PHP中list方法用法示例10-20
PHP中redis的用法深入分析10-17
php中in-array函数用法分析01-18
PHP中final关键字用法08-21
PHP中for循环语句的几种“变态”用法09-29
- 相关推荐