Home >Backend Development >PHP Tutorial >Summarize and organize magic methods in php
This article brings you relevant knowledge about PHP, which mainly introduces related issues about magic methods, and summarizes the __construct construction method, __destruct destructor method, and __clone cloning method. Wait, I hope it helps everyone.
Recommended study: "PHP Video Tutorial"
Detailed explanation of magic methods in php, in php There is a type of method that is very strange, that is, as long as certain conditions are met, it will be executed by itself, that is the magic method in php,
<?php class autofelix { public function __construct() { echo '我是类autofelix'; } } new autofelix(); //即可输出:我是类autofelix
<?php class autofelix { public function __destruct() { echo '我准备销毁你了'; } } $a = new autofelix(); unset($a); //即可输出:我准备销毁你了
<?php class autofelix { public function __clone() { echo '我克隆了你'; } } $a = new autofelix(); clone $a; //即可输出:我克隆了你
<?php class autofelix { private function say() { echo 'hello, 我是autofelix'; } public function __call($name, $arguments) { echo '你无权调用' . $name . '方法'; die; } } $a = new autofelix(); $a->say(); //按理说应该报错 //即可输出:你无权调用say方法
<?php class autofelix { private static function say() { echo 'hello, 我是autofelix'; } public function __callStatic($name, $arguments) { echo '你无权调用' . $name . '方法'; die; } } $a = new autofelix(); $a::say(); //按理说应该报错 //即可输出:你无权调用say方法
<?php class autofelix { public function __debugInfo() { echo '你看不到我任何信息的~'; } } var_dump(new autofelix()); //即可输出:你看不到我任何信息的~
<?php class autofelix { private $name = 'autofelix'; public function __get($name) { if(in_array($name, ['name', 'age'])) { echo $this->name; } else { echo '不是什么东西都能访问的~'; } } } $a = new autofelix(); $a->name; //即可输出:autofelix
<?php class autofelix { private $name = 'autofelix'; public function __isset($name) { if(in_array($name, ['name', 'age'])) { echo $this->name; } else { echo '不是什么东西都能访问的~'; } } } $a = new autofelix(); isset($a->name); //结果: autofelix
<?php class autofelix { public function __set($name, $value) { echo '你想给' . $name . '赋值' . $value; } } $a = new autofelix(); $a->name = 'autofelix'; //结果: 你想给name赋值autofelix;
<?php class autofelix { public function __invoke() { echo '你还想调用我?'; } } $a = new autofelix(); //对象直接当函数调用 $a(); //结果: 你还想调用我?
<?php class autofelix { public function __sleep() { echo '弄啥嘞~'; } } $a = new autofelix(); serialize($a); //结果: 弄啥嘞~
<?php class autofelix { public function __toString() { return '我是你得不到的对象...'; } } $a = new autofelix(); echo $a; //结果: 我是你得不到的对象...
<?php class autofelix { private $name = 'autofelix'; public function __unset($name) { echo '想删我? 你也配?'; } } $a = new autofelix(); unset($a->name); //结果: 想删我? 你也配?
<?php class autofelix { public function __wakeup() { echo '又想弄啥嘞~'; } } $a = new autofelix(); unserialize($a); //结果: 又想弄啥嘞~
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Summarize and organize magic methods in php. For more information, please follow other related articles on the PHP Chinese website!