Home> php教程> php手册> body text

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

WBOY
Release: 2016-11-16 10:24:02
Original
1234 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.

php class Person{ private $name; private $sex; private $age; function __construct($name="",$sex="",$age=1){ $this->name= $name; $this->sex = $sex; $this->age = $age; } function say(){ echo "我的名字:" .$this->name.",性别:".$this->sex.",年龄:".$this->age."
"; } } $p1 = new Person('张三','男','20'); $p2 = clone $p1; $p1->say();//我的名字:张三,性别:男,年龄:20 $p2->say();//我的名字:张三,性别:男,年龄:20 ?>
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)

php function bool2str($bool) { if ($bool === false) { return 'FALSE'; } else { return 'TRUE'; } } function compareObjects(&$o1, &$o2) { echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n"; echo 'o1 != o2 : ' . bool2str($o1 != $o2) . "\n"; echo 'o1 === o2 : ' . bool2str($o1 === $o2) . "\n"; echo 'o1 !== o2 : ' . bool2str($o1 !== $o2) . "\n"; } class Flag { public $flag; function Flag($flag = true) { $this->flag = $flag; } } class OtherFlag { public $flag; function OtherFlag($flag = true) { $this->flag = $flag; } } $o = new Flag(); $p = new Flag(); $q = $o; $r = new OtherFlag(); /* Two instances of the same class o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : FALSE o1 !== o2 : TRUE */ echo "Two instances of the same class\n"; compareObjects($o, $p); /* Two references to the same instance o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : TRUE o1 !== o2 : FALSE */ echo "\nTwo references to the same instance\n"; compareObjects($o, $q); /* Instances of two different classes o1 == o2 : FALSE o1 != o2 : TRUE o1 === o2 : FALSE o1 !== o2 : TRUE */ echo "\nInstances of two different classes\n"; compareObjects($o, $r); ?>
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

php // classa.inc: class A { public $one = 1; public function show_one() { echo $this->one; } } // page1.php: include("classa.inc"); $a = new A; $s = serialize($a); // 把变量$s保存起来以便文件page2.php能够读到 file_put_contents('store', $s); // page2.php: include("classa.inc"); $s = file_get_contents('store'); $a = unserialize($s); // 现在可以使用对象$a里面的函数 show_one() $a->show_one(); ?>
Copy after login

json

json_encode

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
Copy after login

 The json_encode() method performs JSON encoding on variables

php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr);//{"a":1,"b":2,"c":3,"d":4,"e":5} ?>
Copy after login

json_decode

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 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

php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; /* object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } */ var_dump(json_decode($json)); /* array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } */ var_dump(json_decode($json, true)); ?>
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
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!