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,
1. __construct construction method
- It will be called when the class is instantiated
- Simply put, when a new class is created, this method will be automatically executed
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
class autofelix
{
public function __construct()
{
echo '我是类autofelix';
}
}
new autofelix();
|
Copy after login
2. __destruct destructor method
- Automatically triggered when the class is destroyed
- You can use the unset method to trigger this method
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
class autofelix
{
public function __destruct()
{
echo '我准备销毁你了';
}
}
$a = new autofelix();
unset( $a );
|
Copy after login
3. __clone cloning method
- Will be automatically called when the class is cloned
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
class autofelix
{
public function __clone()
{
echo '我克隆了你';
}
}
$a = new autofelix();
clone $a ;
|
Copy after login
4. __call non-static calling method
- It will be automatically called when the method to be called does not exist or has insufficient permissions
- For example, if I call a private modified method inside the class from outside the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php
class autofelix
{
private function say()
{
echo 'hello, 我是autofelix';
}
public function __call( $name , $arguments )
{
echo '你无权调用' . $name . '方法';
die ;
}
}
$a = new autofelix();
$a ->say();
|
Copy after login
5. __callStatic static calling method
- When the static method to be called does not exist or the permissions are insufficient, it will be called automatically.
- For example, when I am in the class Externally call the private modified static method inside the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php
class autofelix
{
private static function say()
{
echo 'hello, 我是autofelix';
}
public function __callStatic( $name , $arguments )
{
echo '你无权调用' . $name . '方法';
die ;
}
}
$a = new autofelix();
$a ::say();
|
Copy after login
6. __debugInfo printing method
- This method will be used when var_dump() class object Called
- If this method is not defined, var_dump() will print out all class attributes
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
class autofelix
{
public function __debugInfo()
{
echo '你看不到我任何信息的~';
}
}
var_dump( new autofelix());
|
Copy after login
7. __get method to obtain member attributes
- Through it, private member properties can be obtained outside the object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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;
|
Copy after login
8. __isset method
- When the pair is inaccessible The attribute will be automatically called when calling isset() or empty()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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);
|
Copy after login
9. __set method
- gives an undefined will be triggered when the attribute is assigned
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
class autofelix
{
public function __set( $name , $value )
{
echo '你想给' . $name . '赋值' . $value ;
}
}
$a = new autofelix();
$a ->name = 'autofelix' ;
|
Copy after login
10. __invoke method
- The object itself cannot be used directly as a function
- If This method will be triggered when the object is called as a function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php
class autofelix
{
public function __invoke()
{
echo '你还想调用我?';
}
}
$a = new autofelix();
$a ();
|
Copy after login
11. __sleep method
- When serialize() is called outside the class It will be called automatically
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php
class autofelix
{
public function __sleep()
{
echo '弄啥嘞~';
}
}
$a = new autofelix();
serialize( $a );
|
Copy after login
12. __toString method
- What should be returned when a class is treated as a string
- A string type must be returned here, otherwise a fatal error will be reported
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
class autofelix
{
public function __toString()
{
return '我是你得不到的对象...';
}
}
$a = new autofelix();
echo $a ;
|
Copy after login
13. __unset method
- When inaccessible The attribute will be automatically called when unset() is called
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php
class autofelix
{
private $name = 'autofelix';
public function __unset( $name )
{
echo '想删我? 你也配?';
}
}
$a = new autofelix();
unset( $a ->name);
|
Copy after login
14. __wakeup method
- will be automatically called when the unserialize() method is executed Call
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php
class autofelix
{
public function __wakeup()
{
echo '又想弄啥嘞~' ;
}
}
$a = new autofelix();
unserialize( $a );
|
Copy after login
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!