【Cloneand __clone "1. The object is a reference data type:when using= When an object is assigned to another object, the address of the object is assigned, and two variables point to the same address.one follows the other and then changes .$lisi=$zhangsan; 2.Clone:If you want to completely clone an object to a non-interfering object,need to use the Clone keyword$lisi=Clone $zhangsan; 3, __clone ():① When cloning an object using the Clone keyword,The Magic Method ②__clone () function will be called automatically, which is equivalent to the constructor when cloning an object,used to assign an initial value to a new cloned object ③ in the Clone () function,$thisRefers to the newly cloned object "__tostring ()" When using theEchoWhen the output statement is printed, the __tostring () method is called when you print the object directly.the string content returned by the Print methodfunction__tostring () {return"[name:{$this->name},age:{$this->age}] ";//array format [name:zhangsan,age:14]//return "{name:{$this->name},age:{$this->age}}";//Object Format {NA ME:ZHANGSAN,AGE:14} } Echo $zhangsan;//echo can only print stringswhen "__call ()" Calls a method that is not exposed or defined in a class,Automatic execution of the __call () method when it is called automatically, two parameters will be passed to the __call () method:① called method name ② the argument passed when calling the method,array Format "__autoload ()" ① This is the only pattern method that does not need to be called in a class ② when instantiating a nonexistent class,This magic method will be called automatically when the ③ method is called, a parameter is automatically passed to __autoload (),represents the instantiated class name, so, which can be based on the class name,automatically import the corresponding class file (class name must be canonical)function__autoload ($class){ include"class/".Strtolower($class).". Class.php "; } $zhangsan=NewPerson ();//there is no person class in this file and will automatically execute __autoload ()and load "class/person.class.php""serialization and crossdress of objects (serialized and deserialized)"1, serialization: The object through a series of operations,convert to a string2, Crossdress: Will serialize after the string,re-convert to object3. When to use serialization:① when an object needs to be transmitted over a network ② when an object needs to persist in a file or database4, how to serialize the object and Crossdress ① serialization.:$str=Serialize($zhangsan); ② crossdress of the line:$obj=unserialize($str); 5, __sleep () Magic Method:① When the execution object is serialized,The __sleep () method is automatically executed ②__sleep () method requires an array to be returned, the values in the array are the properties that can be serialized, not the values in the array,will not be serializablefunction__sleep () {return["Name"];//only the Name property, which can be serialized } 6, __wakeup () Magic Method:① When the execution object is crossdress.,automatic execution of ② when automatically called,used to assign a default value to a newly generated object for Crossdress .function__wakeup () {$this->name = "Lisi";//$this Point to crossdress the newly generated object} "Json_encode and Json_decode serialization of arrays and objects"1. Json_encode: Object and Array,Serialize to a JSON string$str= Json_encode ($arr); 2, Json_decode: The JSON string,crossdress The first parameter of an object or array.:as a JSON string, the second argument: TRUE orfalse, true to convert to an array,falserepresents a transition to an object (default)$obj= Json_decode ($str); $arr= Json_decode ($str,true); "Type constraint"1, type constraint: Before the variable with the data type, for the constraint variable can only hold the value of the corresponding data type (this type of operation is very common in the subtype language, in PHP,You can only implement constraints on arrays and objects that belong to a conforming type,cannot constrain scalar and special types)2, in PHP, type constraints, can only occur in the function of the formal parameters, declare ordinary variables,cannot make type constraintfunctionFuncArray $arr) {}√Array $arr= []; X3. If a type is constrained to a class, then all objects of this class or subclass,are available through constraintsclassperson{}classStudentextendsperson{}functionFunc (person$num) {} func (NewPerson ()); √func (NewStudent ()); √func ("1111"); X "Magic Method Small Summary"1, __construct (): constructor, new object, automatically called,used to assign an initial value to an object2, __destruct (): destructor, when an object is destroyed,automatically called3, __set (): When the object's non-public properties assigned value, automatically called; Parameters: Property name,Property Value4, __get (): When reading the object's non-public property assignment, automatically called; Parameter: attribute name; return value:$this-property name; 5, __isset (): When the object private property is detected by using __isset (), it is called automatically; Parameter: attribute name; return value:isset($this-attribute name); 6, __unset (): When the object private property is deleted using __unset (), it is called automatically; Parameter: attribute name; Execute the unset in the method ($this-attribute name); 7. __clone (): When cloning an object using the Clone keyword,to assign an initial value to a new customer-augmentation object.8. __tostring (): Automatically called when the object is printed using the Echo statement; return value:What you need to print9, __call (): When executing the object in the non-public method, the automatic call; Parameters: Call the method name, array format parameter list,return value Random10. __autoload (): The only magic method used outside of the class is called automatically when instantiating a lifeless class. Parameters:instantiated class masterpieces with the:include the class file in the function11, __sleep (): Automatically called when the object is serialized, return value: Array format,represents a property name that can be serialized12, __wakeup (): When the object is deserialized automatically called, function: to the newly generated object to assign the initial value
Objects in PHP