Php magic method-attribute overload method, php magic attribute overload. Php magic method-attribute overload method, php magic attribute overload php has a very magical method, these methods are reserved methods, usually not explicitly called outside, they use the double-down php magic method-the attribute overload method, and the php magic attribute overload method.
Php has a magical class of methods. these methods are reserved and are generally not explicitly called outside. They start with a double underscore, they are called Magic Methods ). Php officially does not recommend defining other methods that start with a double underline.
This section introduces the method for overloading attributes: get/set/isset/unset.
public void __set ( string $name , mixed $value )public mixed __get ( string $name )public bool __isset ( string $name )public void __unset ( string $name )
When these methods are triggeredAccess inaccessible properties.
For example:
1
_properties[$name] = $value;15 }16 17 public function __get($name){18 echo __METHOD__ . ' is called! ' . json_encode(func_get_args()) . "\n";19 return $this->_properties[$name];20 }21 22 public function __isset($name){23 echo __METHOD__ . ' is called! ' . json_encode(func_get_args()) . "\n";24 return isset($this->_properties[$name]);25 }26 27 public function __unset($name){28 echo __METHOD__ . ' is called! ' . json_encode(func_get_args()) . "\n";29 unset($this->_properties[$name]);30 }31 32 public function dump(){33 echo "====================== DUMP START ====================\n";34 echo <<
a36 b: $this->b37 c: $this->c38 d: $this->d\n39 STR;40 foreach($this->_properties as $k => $v){41 echo <<
dump();55 foreach($list as $method){56 for($i = 0 ; $i < strlen($string) ; $i ++){57 $char = $string{$i};58 echo "------ $method : $char ------\n";59 switch($method){60 case 'isset':61 echo json_encode($tmp = isset($obj->$char)) . "\n";62 break;63 case 'get':64 echo json_encode($tmp = $obj->$char) . "\n";65 break;66 case 'set':67 echo json_encode($tmp = $obj->$char = $char . "-val") . "\n";68 break;69 case 'unset':70 unset($obj->$char);71 break;72 default:73 break;74 }75 }76 $obj->dump();77 }
Result output:
====================== DUMP START ==================== a: 0 b: 1 c: 2 d: 3 Property 0: 0====================== DUMP STOP =====================------ isset : a ------Cls::__isset is called! ["a"]false------ isset : b ------Cls::__isset is called! ["b"]false------ isset : c ------true------ isset : d ------true------ isset : e ------Cls::__isset is called! ["e"]false------ isset : f ------Cls::__isset is called! ["f"]false====================== DUMP START ==================== a: 0 b: 1 c: 2 d: 3 Property 0: 0====================== DUMP STOP =====================------ get : a ------Cls::__get is called! ["a"]null------ get : b ------Cls::__get is called! ["b"]null------ get : c ------2------ get : d ------3------ get : e ------Cls::__get is called! ["e"]null------ get : f ------Cls::__get is called! ["f"]null====================== DUMP START ==================== a: 0 b: 1 c: 2 d: 3 Property 0: 0====================== DUMP STOP =====================------ set : a ------Cls::__set is called! ["a","a-val"]"a-val"------ set : b ------Cls::__set is called! ["b","b-val"]"b-val"------ set : c ------"c-val"------ set : d ------"d-val"------ set : e ------Cls::__set is called! ["e","e-val"]"e-val"------ set : f ------Cls::__set is called! ["f","f-val"]"f-val"====================== DUMP START ==================== a: 0 b: 1 c: c-val d: d-val Property 0: 0 Property a: a-val Property b: b-val Property e: e-val Property f: f-val====================== DUMP STOP =====================------ isset : a ------Cls::__isset is called! ["a"]true------ isset : b ------Cls::__isset is called! ["b"]true------ isset : c ------true------ isset : d ------true------ isset : e ------Cls::__isset is called! ["e"]true------ isset : f ------Cls::__isset is called! ["f"]true====================== DUMP START ==================== a: 0 b: 1 c: c-val d: d-val Property 0: 0 Property a: a-val Property b: b-val Property e: e-val Property f: f-val====================== DUMP STOP =====================------ unset : a ------Cls::__unset is called! ["a"]------ unset : b ------Cls::__unset is called! ["b"]------ unset : c ------------ unset : d ------------ unset : e ------Cls::__unset is called! ["e"]------ unset : f ------Cls::__unset is called! ["f"]====================== DUMP START ====================Cls::__get is called! ["c"]Cls::__get is called! ["d"] a: 0 b: 1 c: d: Property 0: 0====================== DUMP STOP =====================------ isset : a ------Cls::__isset is called! ["a"]false------ isset : b ------Cls::__isset is called! ["b"]false------ isset : c ------Cls::__isset is called! ["c"]false------ isset : d ------Cls::__isset is called! ["d"]false------ isset : e ------Cls::__isset is called! ["e"]false------ isset : f ------Cls::__isset is called! ["f"]false====================== DUMP START ====================Cls::__get is called! ["c"]Cls::__get is called! ["d"] a: 0 b: 1 c: d: Property 0: 0====================== DUMP STOP =====================
This method is triggered only when this attribute does not exist (based on the current access permission. ThereforeWe recommend that you use a fixed key value for get/set processing. in particular, do not have access permissions for the corresponding key value.In the above example, a and B may lead to different processing methods within the class, class external, and Subclass. unpredictable return values are generated during development, making it easy to generate bugs.
The empty method cannot call the attribute overload method.
$tmp = $obj->$char = $char . "-val"
In this example, the values of __set itself will be ignored, and _ get will not be called.
In addition, the property overload method must be declared as public, and parameters cannot be passed using references. static properties cannot be reloaded using these methods, and these methods cannot be declared as static.
Of course, the use of magic methods will also affect the reflection system.
Php has a magical class of methods, which are reserved, and are usually not explicitly called outside. they use double downsides...