本文主要介紹物件導向中的一些物件操作
物件複製,又叫物件克隆,可以透過 clone 關鍵字完成
在多數情況下,我們並不需要完全複製一個物件來獲得其中屬性。但有一個情況下確實需要:如果你有一個視窗對象,該對象持有視窗相關的資源。你可能會想複製一個新的窗口,保持所有屬性與原來的窗口相同,但必須是一個新的對象(因為如果不是新的對象,那麼一個窗口中的改變就會影響到另一個窗口)。還有一種情況:如果物件A 中保存著物件B 的引用,當你複製物件A時,你想其中使用的物件不再是物件B 而是B 的一個副本,那麼你必須得到物件A 的副本
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Person{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$sex</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$age</span><span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$name</span>="",<span style="color: #800080;">$sex</span>="",<span style="color: #800080;">$age</span>=1<span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->name= <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->sex = <span style="color: #800080;">$sex</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->age = <span style="color: #800080;">$age</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> say(){ </span><span style="color: #0000ff;">echo</span> "我的名字:" .<span style="color: #800080;">$this</span>->name.",性别:".<span style="color: #800080;">$this</span>->sex.",年龄:".<span style="color: #800080;">$this</span>->age."<br>"<span style="color: #000000;">; } } </span><span style="color: #800080;">$p1</span> = <span style="color: #0000ff;">new</span> Person('张三','男','20'<span style="color: #000000;">); </span><span style="color: #800080;">$p2</span> = <span style="color: #0000ff;">clone</span> <span style="color: #800080;">$p1</span><span style="color: #000000;">; </span><span style="color: #800080;">$p1</span>->say();<span style="color: #008000;">//</span><span style="color: #008000;">我的名字:张三,性别:男,年龄:20</span> <span style="color: #800080;">$p2</span>->say();<span style="color: #008000;">//</span><span style="color: #008000;">我的名字:张三,性别:男,年龄:20</span> ?>
當使用比較運算子(==)比較兩個物件變數時,比較的原則是:如果兩個物件的屬性和屬性值都相等,而且兩個物件是同一個類別的實例,那麼這兩個對象變數相等
而如果使用全等運算子(===),這兩個物件變數一定要指向某個類別的同一個實例(即同一個物件)
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">function</span> bool2str(<span style="color: #800080;">$bool</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$bool</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> 'FALSE'<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span> 'TRUE'<span style="color: #000000;">; } } </span><span style="color: #0000ff;">function</span> compareObjects(&<span style="color: #800080;">$o1</span>, &<span style="color: #800080;">$o2</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> 'o1 == o2 : ' . bool2str(<span style="color: #800080;">$o1</span> == <span style="color: #800080;">$o2</span>) . "\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> 'o1 != o2 : ' . bool2str(<span style="color: #800080;">$o1</span> != <span style="color: #800080;">$o2</span>) . "\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> 'o1 === o2 : ' . bool2str(<span style="color: #800080;">$o1</span> === <span style="color: #800080;">$o2</span>) . "\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> 'o1 !== o2 : ' . bool2str(<span style="color: #800080;">$o1</span> !== <span style="color: #800080;">$o2</span>) . "\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Flag { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$flag</span><span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> Flag(<span style="color: #800080;">$flag</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->flag = <span style="color: #800080;">$flag</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> OtherFlag { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$flag</span><span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> OtherFlag(<span style="color: #800080;">$flag</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->flag = <span style="color: #800080;">$flag</span><span style="color: #000000;">; } } </span><span style="color: #800080;">$o</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Flag(); </span><span style="color: #800080;">$p</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Flag(); </span><span style="color: #800080;">$q</span> = <span style="color: #800080;">$o</span><span style="color: #000000;">; </span><span style="color: #800080;">$r</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> OtherFlag(); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> Two instances of the same class o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : FALSE o1 !== o2 : TRUE </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">echo</span> "Two instances of the same class\n"<span style="color: #000000;">; compareObjects(</span><span style="color: #800080;">$o</span>, <span style="color: #800080;">$p</span><span style="color: #000000;">); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> Two references to the same instance o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : TRUE o1 !== o2 : FALSE </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">echo</span> "\nTwo references to the same instance\n"<span style="color: #000000;">; compareObjects(</span><span style="color: #800080;">$o</span>, <span style="color: #800080;">$q</span><span style="color: #000000;">); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> Instances of two different classes o1 == o2 : FALSE o1 != o2 : TRUE o1 === o2 : FALSE o1 !== o2 : TRUE </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">echo</span> "\nInstances of two different classes\n"<span style="color: #000000;">; compareObjects(</span><span style="color: #800080;">$o</span>, <span style="color: #800080;">$r</span><span style="color: #000000;">); </span>?>
物件是一種在記憶體中儲存的資料類型,它的壽命通常隨著產生該物件的程式終止而終止。有時候可能需要將物件的狀態保存下來,需要時再將物件恢復。物件透過寫出描述自己狀態的數值來記錄自己,這個過程稱為物件的串列化(Serialization)。以下兩種情況需要將物件串列化:1、物件需要在網路中傳輸時,將物件串列化成二進位串即可;2、物件需要持久保存時,將物件串列化後寫入檔案或資料庫
serialize()
serialize() -- 串列化,傳回一個包含位元組流的字串
unserialize()
unserialize() -- 反串列化,能夠重新把字串變回php原來的物件值
串化一個物件將會保存物件的所有屬性變數和類別名稱訊息,但是不會保存物件的方法
<?<span style="color: #000000;">php </span><span style="color: #008000;">//</span><span style="color: #008000;"> classa.inc:</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> A { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$one</span> = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> show_one() { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">one; } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> page1.php:</span> <span style="color: #0000ff;">include</span>("classa.inc"<span style="color: #000000;">); </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> A; </span><span style="color: #800080;">$s</span> = <span style="color: #008080;">serialize</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 把变量$s保存起来以便文件page2.php能够读到</span> <span style="color: #008080;">file_put_contents</span>('store', <span style="color: #800080;">$s</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> page2.php:</span> <span style="color: #0000ff;">include</span>("classa.inc"<span style="color: #000000;">); </span><span style="color: #800080;">$s</span> = <span style="color: #008080;">file_get_contents</span>('store'<span style="color: #000000;">); </span><span style="color: #800080;">$a</span> = <span style="color: #008080;">unserialize</span>(<span style="color: #800080;">$s</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 现在可以使用对象$a里面的函数 show_one()</span> <span style="color: #800080;">$a</span>-><span style="color: #000000;">show_one(); </span>?>
json_encode
<span style="color: #0000ff;">string</span> json_encode ( <span style="color: #0000ff;">mixed</span> <span style="color: #800080;">$value</span> [, int <span style="color: #800080;">$options</span> = 0 [, int <span style="color: #800080;">$depth</span> = 512 ]] )
json_encode()方法對變數進行 JSON 編碼
<?<span style="color: #000000;">php </span><span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span> ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> json_encode(<span style="color: #800080;">$arr</span>);<span style="color: #008000;">//</span><span style="color: #008000;">{"a":1,"b":2,"c":3,"d":4,"e":5}</span> ?>
json_decode
<span style="color: #0000ff;">mixed</span> json_decode ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$json</span> [, bool <span style="color: #800080;">$assoc</span> = <span style="color: #0000ff;">false</span> [, int <span style="color: #800080;">$depth</span> = 512 [, int <span style="color: #800080;">$options</span> = 0 ]]] )
json_decode()方法對 JSON 格式的字串進行解碼,接受一個 JSON 編碼的字串並且把它轉換為 PHP 變量,當assoc參數為 TRUE 時,將傳回 array 而非 object
<?<span style="color: #000000;">php </span><span style="color: #800080;">$json</span> = '{"a":1,"b":2,"c":3,"d":4,"e":5}'<span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;"> object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } <span style="color: #008000;">*/</span> <span style="color: #008080;">var_dump</span>(json_decode(<span style="color: #800080;">$json</span><span style="color: #000000;">)); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } </span><span style="color: #008000;">*/</span> <span style="color: #008080;">var_dump</span>(json_decode(<span style="color: #800080;">$json</span>, <span style="color: #0000ff;">true</span><span style="color: #000000;">)); </span>?>