Home  >  Article  >  Backend Development  >  Summarize and organize magic methods in php

Summarize and organize magic methods in php

WBOY
WBOYforward
2022-04-15 12:50:225246browse

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.

Summarize and organize magic methods in php

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
<?php
class autofelix 
{
    public function __construct()
    {
        echo &#39;我是类autofelix&#39;;
    }
}

new autofelix();

//即可输出:我是类autofelix

2. __destruct destructor method

  • Automatically triggered when the class is destroyed
  • You can use the unset method to trigger this method
<?php
class autofelix 
{
    public function __destruct()
    {
        echo &#39;我准备销毁你了&#39;;
    }
}

$a = new autofelix();
unset($a);

//即可输出:我准备销毁你了

3. __clone cloning method

  • Will be automatically called when the class is cloned
<?php
class autofelix 
{
    public function __clone()
    {
        echo &#39;我克隆了你&#39;;
    }
}

$a = new autofelix();
clone $a;

//即可输出:我克隆了你

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
<?php
class autofelix 
{
    private function say() 
    {
        echo &#39;hello, 我是autofelix&#39;;
    }

    public function __call($name, $arguments)
    {
        echo &#39;你无权调用&#39; . $name . &#39;方法&#39;;
        die;
    }
}

$a = new autofelix();
$a->say(); //按理说应该报错

//即可输出:你无权调用say方法

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
<?php
class autofelix 
{
    private static function say() 
    {
        echo &#39;hello, 我是autofelix&#39;;
    }

    public function __callStatic($name, $arguments)
    {
        echo &#39;你无权调用&#39; . $name . &#39;方法&#39;;
        die;
    }
}

$a = new autofelix();
$a::say(); //按理说应该报错

//即可输出:你无权调用say方法

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
<?php
class autofelix 
{
    public function __debugInfo()
    {
        echo &#39;你看不到我任何信息的~&#39;;
    }
}

var_dump(new autofelix());

//即可输出:你看不到我任何信息的~

7. __get method to obtain member attributes

  • Through it, private member properties can be obtained outside the object
<?php
class autofelix 
{
    private $name = &#39;autofelix&#39;;

    public function __get($name)
    {
        if(in_array($name, [&#39;name&#39;, &#39;age&#39;])) {
           echo $this->name;
        } else {
            echo '不是什么东西都能访问的~';
        }
    }
}

$a = new autofelix();
$a->name;

//即可输出:autofelix

8. __isset method

  • When the pair is inaccessible The attribute will be automatically called when calling isset() or empty()
<?php
class autofelix 
{
    private $name = &#39;autofelix&#39;;

    public function __isset($name)
    {
        if(in_array($name, [&#39;name&#39;, &#39;age&#39;])) {
           echo $this->name;
        } else {
            echo '不是什么东西都能访问的~';
        }
    }
}

$a = new autofelix();
isset($a->name);

//结果: autofelix

9. __set method

  • gives an undefined will be triggered when the attribute is assigned
<?php
class autofelix 
{
    public function __set($name, $value)
    {
        echo &#39;你想给&#39; . $name . &#39;赋值&#39; . $value;
    }
}

$a = new autofelix();
$a->name = 'autofelix';

//结果: 你想给name赋值autofelix;

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
<?php
class autofelix 
{
    public function __invoke()
    {
        echo &#39;你还想调用我?&#39;;
    }
}

$a = new autofelix();

//对象直接当函数调用
$a();

//结果: 你还想调用我?

11. __sleep method

  • When serialize() is called outside the class It will be called automatically
<?php
class autofelix 
{
    public function __sleep()
    {
        echo &#39;弄啥嘞~&#39;;
    }
}

$a = new autofelix();

serialize($a);

//结果: 弄啥嘞~

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
<?php
class autofelix 
{
    public function __toString()
    {
        return &#39;我是你得不到的对象...&#39;;
    }
}

$a = new autofelix();
echo $a;

//结果: 我是你得不到的对象...

13. __unset method

  • When inaccessible The attribute will be automatically called when unset() is called
<?php
class autofelix 
{
    private $name = &#39;autofelix&#39;;

    public function __unset($name)
    {
        echo &#39;想删我? 你也配?&#39;;
    }
}

$a = new autofelix();
unset($a->name);

//结果: 想删我? 你也配?

14. __wakeup method

  • will be automatically called when the unserialize() method is executed Call
<?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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete