The fifth article in the object-oriented series of PHP-object operations and the fifth article in the object-oriented Series

Source: Internet
Author: User
Tags object serialization

The fifth article in the object-oriented series of PHP-object operations and the fifth article in the object-oriented Series
* Directory [1] object clone [2] object comparison [3] Object serialization [4] Before json

This article mainly introduces some object operations in the object-oriented model.

 

Object cloning

Object replication, also called object cloning, can be completed through the clone keyword

In most cases, we do not need to completely copy an object to obtain its attributes. However, you need to: If you have a window object, the object holds window-related resources. You may want to copy a new window to keep all attributes the same 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 ). Another case is: If object A contains the reference of object B, when you copy object A, you want to use the object instead of object B but A copy of object B, then you must get A copy of object.

<? 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 "my name: ". $ this-> name. ", Gender :". $ this-> sex. ", age :". $ this-> age. "<br>" ;}}$ p1 = new Person ('zhang san', 'mal', '20'); $ p2 = clone $ p1; $ p1-> say (); // my name: Zhang San, Gender: male, age: 20 $ p2-> say (); // my name: Zhang San, gender: male, age: 20?>

 

Object comparison

When the comparison operator (=) is used 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, the two object variables are equal.

If the full equality operator (=) is used, these two object variables must point to the same instance of a class (that is, the same object)

<?phpfunction 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 classo1 == o2 : TRUEo1 != o2 : FALSEo1 === o2 : FALSEo1 !== o2 : TRUE */echo "Two instances of the same class\n";compareObjects($o, $p);/*Two references to the same instanceo1 == o2 : TRUEo1 != o2 : FALSEo1 === o2 : TRUEo1 !== o2 : FALSE */echo "\nTwo references to the same instance\n";compareObjects($o, $q);/*Instances of two different classeso1 == o2 : FALSEo1 != o2 : TRUEo1 === o2 : FALSEo1 !== o2 : TRUE */echo "\nInstances of two different classes\n";compareObjects($o, $r);?>

 

Object serialization

An object is a type of data stored in the memory. Its life ends with the termination of the program that generates the object. Sometimes you may need to save the object state and restore the object as needed. An object Records itself by writing a value describing its own State. This process is called Serialization ). In the following two cases, objects must be serialized: 1. objects must be serialized into binary strings when they are transmitted over the network; 2. objects must be persistently saved, serializes objects and writes them to a file or database.

Serialize ()

Serialize () -- serialized, returns a string containing byte streams

Unserialize ()

Unserialize () -- returns the string to the original object Value of php.

Serializing an object will save all the property variables and Class Name Information of the object, but will not save the object's Method

<? 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); // Save the variable $ s so that the file page2.php can read file_put_contents ('store ', $ s); // page2.php: include ("classa. inc "); $ s = file_get_contents ('store'); $ a = unserialize ($ s); // you can now use the show_one () function in the object $ () $ a-> show_one ();?>

 

Json

Json_encode

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

The json_encode () method is used to encode the variables in JSON format.

<?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}?>

Json_decode

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

The json_decode () method decodes JSON strings, accepts a JSON-encoded string, and converts it to a PHP variable. When the assoc parameter is TRUE, array instead of object is returned.

<?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));?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.