本文主要介绍面向对象中的一些对象操作
对象复制,又叫对象克隆,可以通过 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><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>?>