Front-end Learning PHP - Part 5 of the Object-Oriented Series - Object Operations
Nov 16, 2016 am 10:24 AMPrevious words
This article mainly introduces some object operations in object-oriented
Object Clone
Object copying, also called object cloning, can be accomplished through the clone keyword
In most cases, we do not need to completely copy an object to obtain its properties. But there is one situation where it is really needed: if you have a window object that holds window-related resources. You may want to copy a new window, keeping all the same properties as the original window, but it must be a new object (because if it is not a new object, changes in one window will affect the other window). There is another situation: if object A stores a reference to object B, when you copy object A, and you want the object used in it to be no longer object B but a copy of B, then you must get a copy of object 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> ?>
Object comparison
When using the comparison operator (==) to compare two object variables, the comparison principle is: if the attributes and attribute values ??of the two objects are equal, and the two objects are instances of the same class, then the two objects Variables are equal
If you use the equality operator (===), these two object variables must point to the same instance of a certain class (that is, the same object)
<?<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>?>
Object serialization
An object is a data type stored in memory, and its lifespan usually ends when the program that generated the object terminates. Sometimes you may need to save the state of an object and restore the object when needed. Objects record themselves by writing values ??that describe their status. This process is called serialization of objects. The object needs to be serialized in the following two situations: 1. When the object needs to be transmitted over the network, just serialize the object into a binary string; 2. When the object needs to be persisted, serialize the object and write it to a file or database.
serialize()
Serialize() -- Serialization, returns a string containing a byte stream
unserialize()
unserialize() -- Deserialization, can change the string back to the original object value of PHP
Serializing an object will save all attribute variables and class name information of the object, but will not save the object’s methods
<?<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;"> 現(xiàn)在可以使用對象$a里面的函數(shù) show_one()</span> <span style="color: #800080;">$a</span>-><span style="color: #000000;">show_one(); </span>?>
json
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 ]] )
The json_encode() method performs JSON encoding on variables
<?<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 ]]] )
The json_decode() method decodes a JSON-formatted string, accepts a JSON-encoded string and converts it into a PHP variable. When the assoc parameter is TRUE, an array will be returned instead of an 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>?>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)