PHP magic function usage instructions

  • 2020-03-31 20:26:31
  • OfStack

What is a magic function?
The function at the beginning of __ is named magic function, which is triggered under certain conditions, e.g., successive set() and successive get()
Triggered when setting or fetching a nonexistent property.
What are the magic functions?
In general, there are a few magic functions
__set __get __destruct __construct () () () () __isset __unset () () the __call () __callStatic ()
__set_state __toString __wakeup __sleep () () () () __clone __autoload () ()
When instantiating an object, this method of the object is called first.
This method is called when an object is deleted or an object operation terminates.
 
class test1 { 
public function __construct() { 
var_dump(__function__); 
} 
public function __destruct() { 
var_dump(__function__); 
} 
} 
$t1 = new test1 ; 
unset($t1); 

Successive get is called while trying to read a nonexistent property.
S is called while trying to write a value to a nonexistent property.
S/s is called while trying to detect a nonexistent property.
S/s called when attempting to cancel a nonexistent property.
 
class test2 { 
public $name3; 
public function __set($key, $value) { 
var_dump(__function__. ' 
KEY:' 
.$key.' 
Value:' 
.$value); 
} 
public function __get($key) { 
var_dump(__function__. 'KEY:'.$key); 
} 
public function __isset($key) { 
var_dump(__function__. ' KEY:'.$key); 
} 
public function __unset($key) { 
var_dump(__function__. ' KEY:'.$key); 
} 
} 
$t =new test2 ; 
$t->name = "steven"; 
$t->name2; 
$t->name3; 
isset($t->name2); 
isset($t->name3); 
unset($t->name4); 

Called while serializing the object
Called while performing an unsequenced object
One thing to note:
1. S/s () must return an array or object (usually $this). The returned value will be used as the serialization
Value.
If this value is not returned, the serialization fails. This also means that deserialization will not trigger the arbitration event.
Serialization will save the default assigned properties. If you want to instantiate the assigned content, you will need the property to return the values of the array in arbitration sleep()
Specified.
For example, the difference between $id and $id2.
 
class test3 { 
public $name = "steven"; 
public $id = "1"; public $id2; 
public function __sleep() { 
var_dump(__function__); //  Serialization failed . No return value . The inverse sequence also fails  // 
return array("name"); //  Serialization success . Returns a value . Inverse sequence success .id2 Properties can be restored  // 
return array("name", "id2");//Serialization success. Return value. Deserialization success. Id2 attribute cannot be restored
return array("name"); } 
public function testEcho() { 
var_dump($this->name); 
var_dump($this->id); 
var_dump($this->id2); 
} 
public function __wakeup() { 
var_dump(__function__); 
$this->testEcho(); 
} 
} 
$t3= new test3 ; 
$t3->id2 = uniqid(); 
$t3s = serialize($t3); 
unserialize($t3s); 

This method will be called while printing an object directly
 
class test4 { 
public function __toString() { 
return "toString"; 
} 
} 
$t4 = new test4(); 
echo $t4; 
print $t4; 
var_dump($t4); 
print_r($t4); 

S/s call($func, $param) is called when trying to call a nonexistent method.
The method must have two arguments, the first being the name of the called method and the second an array of arguments for the called method.
Note that when you call a parent class's private method in a subclass, or a class's non-protect method in an instance
, you will not call s/s ()
 
class test5 { 
public function __call($func, $param) { 
var_dump('Function:'.$func); 
var_dump($param); 
} 
} 
$t5 = new test5; 
$t5->echoTest('xx','xx','xx'); 

Callcallstatic () is called when attempting to call a nonexistent static method
The method must have two arguments, the first being the name of the called method and the second an array of arguments for the called method.
It appears in PHP5.3
 
class test51 { 
public function __callStatic($fun, $param) { 
var_dump('Function:'.$func); 
var_dump($param); 
} 
} 
test51::test('xx','xx','xx'); 

S _state() is called when the instance is exported with var_export. This method takes one argument to the location that contains the exported instance
An array of member properties
 
class test6 { 
public function __set_state($arr){ 
var_dump($arr); 
} 
} 
$t6 = new test6; 
$t6->age = "12"; 
var_export($t6, true); 
var_export($t6); 
eval(' 
$b=' 
.var_export($t6,true).';'); 
print_r($b); 

S is called while the clone is being cloned.
Note:
1. In php5, assignments between objects are always passed by address reference.
2. If you want to pass in real values, you need to use the clone keyword
3. Clone is an instance only. If a member property in an instance is also an instance, the member property will still be referenced
Pass to the new instance.
// assignments between objects are always passed by address reference. The age attribute of $t71 $t72 is the same.
 
class test71 { 
public $age = 10; 
} 
$t71 = new test71(); 
$t72 = $t71 ; 
var_dump($t71->age) ; 
$t71->age =12 ; 
var_dump($t71->age) ; 
var_dump($t72->age) ; //To pass in the actual value, use the clone keyword $t73 = clone $t71; $t71 -> Age = 13; Var_dump ($t71 -> The age);
var_dump($t73->age) ; //If a member property in an instance is also an instance, the member property is passed to the new instance as a reference method.

 
class test74 { 
public $age = 10; 
public $sub = null; 
} 
class test75 { 
public $age = 11; 
} 
$i = new test74; 
$i->sub = new test75(); 
$i1 =clone $i; 
var_dump($i1->sub->age); 
$i->sub->age = 12; 
var_dump($i1->sub->age); 

// $I and $i1 do not point to the same instance, but their member property, $sub, points to the same instance. At this point, we have to borrow
Help with this method to copy $sub. // $i2 and $3 point to different instances. The member property $sub also points to different instances.
 
class test76 { 
public $age = 10; 
public $sub = null; 
public function __clone() { 
$this->sub = clone $this->sub; 
} 
} 
$i2 = new test76(); 
$i2->sub = new test75(); 
$i3 = clone $i2; 
$i2->sub->age = 15 ; 
var_dump($i3->sub->age); 

S) function. When creating an instantiation, if the corresponding class does not exist, it will be called
 
function __autoload($class) { 
   if ( $class == "test8" ){ 
require_once dirname(__FILE__).'/class8.php';  
} 
} 
spl_autoload(); 
$t8 = new test8; 
var_dump($t8->age); 

Related articles: