Home > php教程 > php手册 > Front-end Learning PHP - Part 5 of the Object-Oriented Series - Object Operations

Front-end Learning PHP - Part 5 of the Object-Oriented Series - Object Operations

WBOY
Release: 2016-11-16 10:24:02
Original
1299 people have browsed it
×
Table of Contents
[1] Object cloning [2] Object comparison [3] Object serialization [4] json

Previous 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>
?>
Copy after login

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>?>
Copy after login

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;"> 现在可以使用对象$a里面的函数 show_one()</span>
  <span style="color: #800080;">$a</span>-><span style="color: #000000;">show_one();
</span>?>
Copy after login

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 ]] )
Copy after login

 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>
?>
Copy after login

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 ]]] )
Copy after login

 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>?>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template