- ----- the serialize the instantiated objects written to the file
- __sleep triggered when calling serialize
<?php
class mycoach
{
public function __construct($name,$age,$expertin=[]){
$this->name = $name;
$this->age = $age;
$this->expertin=[];
$this->expertin=$expertin;
}
public function __sleep()
{
return ['name','age','expertin'];
}
}
CPC $ = new new mycoach ( ' Chenpei Chang ' , 22 , [ ' Sanda ' , ' Thai boxing ' , ' BJJ ' ]);
$ srobj = the serialize ($ CPC);
file_put_contents ( ' cpcssecret.txt ' , $ srobj);
?>
key points:
internal ---- class implementation __sleep () to return an array of data structures, elements from all classes of property, in order to achieve what type of control can be written to the file
---- serialize method object as a parameter the return value is the data file to be written.
Generating an object file recorded in the form of:
O: 7: "mycoach": 3: {s: 4: "name"; s: 9: "Chen Peichang"; s: 3: "age"; i: 22; s: 8: "expertin"; a: 3 : {i: 0; s: 6: "Sanda"; i: 1; s: 6: "Thai boxing"; i: 2; s: 12: "BJJ";}}
- ----- to unserialize recording file restore instance of the object class
- Called when execution unserialize __wakeup ------, to perform some initialization
<?php
class mycoach
{
public function __construct($name,$age,$expertin=[]){
$this->name = $name;
$this->age = $age;
$this->expertin=[];
$this->expertin=$expertin;
}
public function __sleep()
{
return ['name','age','expertin'];
}
publicthe __wakeup function ()
{
# Usage: to restore an object (deserialization) time, to be initialized
echo " restore object " . " \ n- " ;
}
}
$ objdate = file_get_contents ( ' cpcssecret.txt ' );
var_dump ( to unserialize ($ objdate));
>?
output:
restore object
Object (mycoach) #. 1 (. 3) {
[ "name"] =>
String (. 9) "Chen Peichang"
[ "Age"] =>
int (22 is)
[ "expertin"] =>
Array (. 3) {
[0] =>
String (. 6) "Sanda"
[. 1] =>
String (. 6) "Thai boxing"
[2] =>
String (12 is) "BJJ"
}
}
- clone copy object properties
- __clone can restrict which properties can be copied, which uses a custom property
- __autoload only way to use the class definition vitro