<?php class MagicTest{ //__tostring会在把<strong>对象</strong>转换为string的时候自动调用 public function __tostring(){ return "This is the Class MagicTest. "; } // __invoke会在把<strong>对象</strong>当作一个方法调用的时候自动调用 public function __invoke($x){ echo "__invoke called with parameter ".$x."\n"; } } $obj = new MagicTest(); echo $obj."\n"; $obj(5); ?>
This is the Class MagicTest.
__invoke called with parameter 5
When the
object accesses a non-existing method name, the __call() method will be automatically called __callStatic()
When the
object accesses a non-existing static method name When Can be implemented in different ways
<?php class MagicTest{ public function __tostring(){ return "This is the Class MagicTest. "; } public function __invoke($x){ echo "__invoke called with parameter ".$x."\n"; } //方法的重载 //这个方法的参数第一个就是调用的方法的名称,第二个参数是方法调用的参数组成的数组 public function __call($name,$arguments){ echo "Calling " . $name . " with parameters: ". implode(",", $arguments)."\n"; } //<strong>静态方法</strong>的重载,注意这个方法需要设定为static public static function __callStatic($name,$arguments){ echo "Static Calling " . $name . " with parameters: ". implode(",", $arguments)."\n"; } } $obj = new MagicTest(); //runTest这两个方法的名称本来不能完全相同,但是通过魔术方法,可以调用了 $obj->runTest("para1","para2"); //没有声明这个runTest方法,因为有__call这个魔术方法,也可以被调用 MagicTest::runTest("para1","para2"); //没有声明这个runTest方法,因为有__callStatic这个魔术方法,也可以被调用 ?>
__get(), __set(), __isset(), __unset( )
When assigning a value to an inaccessible property, __set() will be called
When calling isset() or empty() on an inaccessible property, __isset( ) will be called
When unset() is called on an inaccessible attribute, __unset() will be called
The so-called inaccessible attribute actually means that when calling a certain attribute, it is found that the attribute is not defined. At this time, different operations will Trigger different magic methods
These methods are also called magic methods of property overloading
<?php class MagicTest{ public function __tostring(){ return "This is the Class MagicTest. "; } public function __invoke($x){ echo "__invoke called with parameter ".$x."\n"; } //方法的重载 //这个方法的参数第一个就是调用的方法的名称,第二个参数是方法调用的参数组成的数组 public function __call($name,$arguments){ echo "Calling " . $name . "with parameters: ". implode(",", $arguments)."\n"; } //<strong>静态方法</strong>的重载,注意这个方法需要设定为static public static function __callStatic($name,$arguments){ echo "Static Calling " . $name . "with parameters: ". implode(",", $arguments)."\n"; } //属性重载 public function __get($name){ return "Getting the property ".$name."\n"; } public function __set($name, $value){ echo "Setting the property ".$name." to value " . $value."\n"; } public function __isset($name){ echo "__isset invoked\n"; return true; } public function __unset($name){ echo "unsetting property ".$name."\n"; } } $obj = new MagicTest(); echo $obj->className."\n"; //className未定义,但是通过魔术方法__get,这个方法好像被定义了一样 $obj->className='MagicClassX'; //通过魔术方法__get将className名称定义为MagicClassX echo '$obj->name is set?'.isset($obj->className)."\n"; echo '$obj->name is empty?'.empty($obj->className)."\n"; unset($obj->className); ?>
Setting the property className to value MagicClassX
__isset invoked
$obj-> name is set?1 //If return is true, the result is displayed as 1; if return is false, the result is displayed as empty
__isset invoked
$obj->name is empty? //If return is true, the result is displayed Displayed as empty; if return is false, the result is displayed as 1
unsetting property className
oriented
object
--magic method