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