|
<? Php
Class Magic
{
Public $ var = 'test ';
// Constructor called when an object is created
Public function _ construct ()
{
Echo '_ construct called'. PHP_EOL;
}
// All references to an object are deleted, the object is destroyed, and after exit () is called, or when the script is closed
Public function _ destruct ()
{
Echo '_ destruct called'. PHP_EOL;
}
// Called when a value is assigned to an inaccessible or non-existent attribute
Public function _ set ($ name, $ value)
{
Echo $ name. '-'. $ value;
Echo '_ set called'. PHP_EOL;
}
// It is called to read inaccessible or non-existent attributes.
Public function _ get ($ name)
{
Echo $ name;
Echo '_ get called'. PHP_EOL;
}
// Call a method that is inaccessible or does not exist
Public function _ call ($ name, $ arguments)
{
Echo $ name. '-'. implode (',', $ arguments );
Echo '_ call called'. PHP_EOL;
}
// Call an inaccessible or non-existent static method
Public static function _ callStatic ($ name, $ arguments)
{
Echo $ name. '-'. implode (',', $ arguments );
Echo '_ callStatic called'. PHP_EOL;
}
// Isset () or empty () is called when the attribute is inaccessible or does not exist.
Public function _ isset ($ name)
{
Echo $ name;
Echo '_ isset called'. PHP_EOL;
Return true;
}
// Called when unset is performed on inaccessible or non-existent attributes
Public function _ unset ($ name)
{
Echo $ name;
Echo '_ unset called'. PHP_EOL;
}
// It is called when serialize is used. It is useful when you do not need to save all data of a large object.
Public function _ sleep ()
{
Echo '_ sleep called'. PHP_EOL;
Return array ('var11111111111111 ');
}
// Unserialize is called and can be used to initialize objects.
Public function _ wakeup ()
{
Echo '_ wakeup called'. PHP_EOL;
$ This-> var = 'test after wakeup ';
}
// Called when a class is converted to a string
Public function _ toString ()
{
Return '_ toString called'. PHP_EOL;
}
// Called for object cloning to adjust the object cloning Behavior
Public function _ clone ()
{
Echo '_ clone called'. PHP_EOL;
}
// Called when an object is called in function mode
Public function _ invoke ()
{
Echo '_ invoke called'. PHP_EOL;
}
// This static method is called when var_export () is called to export the class. Use the return value of _ set_state as the return value of var_export.
Public static function _ set_state ($ arr)
{
Return '_ set_state called'. PHP_EOL;
}
// It is called when var_dump () is called to print the object (when you do not want to print all attributes) applicable to PHP5.6
Public function _ debuginfo ($ arr)
{
Echo '_ debuginfo called'. PHP_EOL;
Return array (
'Var' => 'test fro _ debuginfo'
);
}
}
$ M = new Magic (); // _ construct () called
$ M-> not_exist_property = test; // _ set () is called
Echo $ m-> not_exist_property; // _ get () called
$ M-> abc (1, 2, 3); // _ call () is called
Echo isset ($ m-> not_exist_property); // _ isset () is called, return the bool Value
Unset ($ m-> not_exist_property); // _ unset () is called
Echo $ tmp = serialize ($ m); // _ sleep () is called
Unserialize ($ tmp); // _ wakeup () is called
$ M1 = clone $ m; // _ clone () is called. objects are passed by reference by default. Object replication can be implemented using the clone keyword.
$ M (); // _ invoke ()
Eval ('$ m2 ='. var_export ($ m, true). ';'); var_dump ($ m2 );
Var_dump ($ m );
// The Last _ destruct () is called.
/*
Result:
_ Construct called
Not_exist_property-test _ set called
Not_exist_property _ get called
Abc-1, 2,3 _ call called
Not_exist_property _ isset called
1not_exist_property _ unset called
_ Sleep called
O: 5: "Magic": 1: {s: 13: "var1111111111"; N ;:__ wakeup called
_ Destruct called
_ Clone called
_ Invoke called
String (20) "_ set_state called
"
Class Magic #1 (1 ){
Public $ var =>
String (4) "test"
}
_ Destruct called
_ Destruct called
*/
|