簡單分析PHP中序列化用法介紹
序列化在我們學習php中都會有用到了對於序列化我們常用的函數有serialize和unserialize了,希望以下這篇文章能夠幫助到各位瞭解到PHP中序列化用法,具體如下:
0x00 序列化函數
serialize():返回帶有變量類型和值的字符串
unserialize():想要將已序列化的字符串變回 PHP 的值
測試代碼:
<?php
class test{
var $a;
var $b;
function __construct($a,$b,$c){
$a = $a;
$this->b = $b;
}
}
class test1 extends test{
function __construct($a){
$this->a = $a;
}
}
$a = 'hello';
$b = 123;
$c = false;
$d = new test('helloa','hellob','helloc');
$e = new test1('hello');
var_dump(serialize($a));
var_dump(serialize($b));
var_dump(serialize($c));
var_dump(serialize($d));
var_dump(serialize($e));
?>
運行結果:
string 's:5:"hello";' (length=12)
string 'i:123;' (length=6)
string 'b:0;' (length=4)
string 'O:4:"test":2:{s:1:"a";N;s:1:"b";s:6:"hellob";}' (length=46)
string 'O:5:"test1":2:{s:1:"a";s:5:"hello";s:1:"b";N;}' (length=46)
序列化字符串格式: 變量類型:變量長度:變量內容 。
如果序列化的是一個對象,序列化字符串格式為:
變量類型:類名長度:類名:屬性數量:{屬性類型:屬性名長度:屬性名;屬性值類型:屬性值長度:屬性值內容}
將上述結果反序列化輸出,執行結果:
string 'hello' (length=5)
int 123
boolean false
object(test)[1]
public 'a' => null
public 'b' => string 'hellob' (length=6)
object(test1)[1]
public 'a' => string 'hello' (length=5)
public 'b' => null
0x01 對象序列化
當序列化對象時,PHP 將在序列動作之前調用該對象的成員函數 sleep()。這樣就允許對象在被序列化之前做任何清除操作。類似的,當使用 unserialize() 恢復對象時, 將調用 wakeup()成員函數。
在serialize()函數執行時,會先檢查類中是否定義了 sleep()函數,如果存在,則首先調用 sleep()函數,如果不存在,就保留序列字符串中的所有屬性。
在unserialize()函數執行時,會先檢查是否定義了 wakeup()函數。如果 wakeup()存在,將執行__wakeup()函數,會使變量被重新賦值。
serialize()測試代碼:
<?php
class test{
var $a;
var $b;
function __construct($a,$b,$c){
$this->a = $a;
$this->b = $b;
}
function __sleep(){
echo "b has changed"."n";
$this->b = 'hib';
return $this->b;
}
function __wakeup(){
echo "a has changed"."n";
$this->a = 'hia';
}
}
class test1 extends test{
function __construct($a){
$this->a = $a;
}
}
$d = new test('helloa','hellob','helloc');
$e = new test1('hello');
serialize($d);
serialize($e);
var_dump($d);
var_dump($e);
?>
執行結果:
b has changed b has changed
object(test)[1]
public 'a' => string 'helloa' (length=6)
public 'b' => string 'hib' (length=3)
object(test1)[2]
public 'a' => string 'hello' (length=5)
public 'b' => string 'hib' (length=3)
unserialize()測試代碼:
class test{
var $a;
var $b;
function __construct($a,$b,$c){
$this->a = $a;
$this->b = $b;
}
function __sleep(){
echo "b has changed"."n";
$this->b = 'hib';
return $this->b;
}
function __wakeup(){
echo "a has changed"."n";
$this->a = 'hia';
}
}
class test1 extends test{
function __construct($a){
$this->a = $a;
}
}
$d = 'O:4:"test":2:{s:1:"a";N;s:1:"b";s:6:"hellob";}' ;
-
WAP的簡介
WAP協議被設計用來在無線客户端(比如移動電話)上展示因特網內容。您應當具備的基礎知識在繼續學習之前,您需要對下面的知識有基本的瞭解:WWW,HTML以及有關網頁構建的基礎知識JavaScriptXML如果您希望首先學習這些項目,請在我們的首頁訪問這些教程。什麼是WAP?無線產...
-
JavaScript數據結構與算法中集合的實現
集合(Set)説起集合,就想起剛進高中時,數學第一課講的就是集合。因此在學習集合這種數據結構時,倍感親切。集合的基本性質有一條:集合中元素是不重複的。因為這種性質,所以我們選用了對象來作為集合的容器,而非數組。雖然數組也能做到所有不重複,但終究過於繁瑣,不如集...
-
ASP.NET新型的投票結果顯示方法
一個投票功能模塊少不了查看投票結果,用進度條顯示各個投票結果可以起到一目瞭然的效果。以下是我的方法,請大家不吝賜教:1:做一張圖片用於做進度條,只需要很小的一個圖片就可以了,如高20px,寬1px。2:在要顯示進度條的單元格中插入image控件,其imageUrl設置為已做好的...
-
如何理解Javascript的caller,callee,call,apply區別
在提到上述的概念之前,首先想説説javascript中函數的隱含參數:argumentsarguments該對象代表正在執行的函數和調用它的函數的參數。[function.]arguments[n]參數function:選項。當前正在執行的Function對象的名字。n:選項。要傳遞給Function對象的從0開始的參數值...