Front-End Learning PHP's object-oriented series fifth--Object manipulation

Source: Internet
Author: User
Tags string back

Object cloning

Object replication, also known as Object cloning, can be done by using the clone keyword

In most cases, we do not need to completely copy an object to get its properties. But there is a case where it really needs to be: If you have a Window object, the object holds the window-related resources. You may want to copy a new window, keeping all properties the same as the original window, but it must be a new object (because if it is not a new object, the change in one window will affect the other window). Another case: If object A holds a reference to object B, when you copy object A, the object you want to use is no longer an 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 "my Name:". $this->name. ", Gender:". $this->sex. ", Age:". $this->age. " <br> ";        }    }    $p 1 = new Person (' Zhang San ', ' Male ', ') ';    $p 2 = clone $p 1;    $p 1->say ();//My Name: Zhang San, gender: Male, Age:    $p 2->say ();//My Name: Zhang San, gender: male, age:20?>

Object comparison

When comparing two object variables with the comparison operator (= =), the principle of comparison is that if the properties and property values of two objects are equal, and two objects are instances of the same class, then the two object variables are equal < $o = new flag (); $p = new Flag (); >

If you use the congruent operator (= = =), the two object variables must point to the same instance of a class (that is, the same object ) $q = $o;

<?phpfunction Bool2str ($bool) {if ($bool = = = False) {return ' false ';    } else {return ' TRUE '; }}function compareobjects (& $o 1, & $o 2) {echo ' O1 = = O2: '. BOOL2STR ($o 1 = = $o 2).    "\ n"; Echo ' O1! = O2: '. BOOL2STR ($o 1! = $o 2).    "\ n"; echo ' O1 = = = O2: '. Bool2str ($o 1 = = = $o 2).    "\ n"; Echo ' O1!== O2: '. Bool2str ($o 1!== $o 2). "\ 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;//If you write $q = Clone $o then = = = Fasle$r = new Otherflag ();/*two instances of the same CLASSO1 = = o2:trueo1! = O2:falseo1 = = O2:falseo1!== o2:true */e Cho "Instances of the same class\n"; Compareobjects ($o, $p);/*two references to the same instanceo1 = = o2:trueo1! = O 2:falseo1 = = = O2:trueo1!== o2:false */echo "\ntwo references to the same instance\n"; Compareobjects ($o, $q);/*insta NCEs of different CLASSESO1 = = o2:falseo1! = O2:trueo1 = = O2:falseo1!== o2:true */echo "\ninstances of the D Ifferent classes\n "; Compareobjects ($o, $r);? >

Serialization of objects

An object is a type of data that is stored in memory, and its lifetime is usually terminated as the program that generated the object terminates. Sometimes it may be necessary to save the state of the object and restore the object when needed. The object records itself by writing a value describing its state, which is called the serialization of the object (serialization). The following two scenarios require serialization of an object: 1, when the object needs to be transferred in the network, the object is serialized into a binary string, 2. When the object needs to be persisted, the object is serialized and written to the file or database

Serialize ()

Serialize ()--serialization, returns a string containing a stream of bytes

Unserialize ()

Unserialize ()--crossdress, able to re-change the string back to the original PHP object value

Serializing an object will save all of the object's property variables and class name information, 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);  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 function Show_one () inside the object $ A ()  $a->show_one ();? >

Json

Json_encode

String Json_encode (mixed $value [, int $options = 0 [, int $depth = 512]])

Json_encode () method to JSON-encode a variable

<?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 = [, int $options = 0]]])

 The Json_decode () method decodes a JSON-formatted string, takes a JSON-encoded string and converts it to a PHP variable, and returns an array instead of an object when the Assoc argument is TRUE

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

Front-End learning PHP Object-oriented series fifth-object operations

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.