Home  >  Article  >  Backend Development  >  What are magic methods in PHP? What are the commonly used magic methods?

What are magic methods in PHP? What are the commonly used magic methods?

慕斯
慕斯Original
2021-06-17 17:44:152727browse

The previous article introduced you to "What is inheritance and derivation in PHP? How do we use inheritance? 》, this article continues to introduce to you what is the magic method in PHP? What are the commonly used magic methods? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What are magic methods in PHP? What are the commonly used magic methods?

#1. What is a magic method

A method that the system automatically calls at a specific time

2. Commonly used magic methods:

_get

Trigger timing: Called when the object accesses private members or protected properties externally

This method has one parameter: the parameter is the attribute name

Let’s take the code as an example:

First we create a new file and write a class Class, define attributes in the class, and then we create an object. When we echo the class just defined, we will find an error because the object can only access public attributes, and we cannot access protected and private ones. Attribute, the code is as follows:

<?php
class Person 
{
    public $name = &#39;林徽因&#39;;
    protected $pome = &#39;答案很长,我准备用一生的时间来回答,你准备要听了吗?&#39;;
    private $time = &#39;民国&#39; ;
}
$niu = new Person();
echo $niu->pome;
?>

The code displays the result:

What are magic methods in PHP? What are the commonly used magic methods?

We will find that there is an error when running our above code. Therefore, the protected cannot be accessed externally. And private properties, if we want to access protected or private member properties through the object externally, the get method will be automatically triggered.

    public function __get($name){
        echo $name;
    }

Then print out $name,

The code displays the result:

What are magic methods in PHP? What are the commonly used magic methods?

So we can use the if statement to judge through the above code :

 if ($name == &#39;pome&#39;){
            return $this->pome;

The code displays the result:

What are magic methods in PHP? What are the commonly used magic methods?

The above is what we call get usage

--set

Trigger timing: Called when the object sets private or protected member attribute values ​​externally

This method has two parameters:

Parameter 1: Member attribute name!

Parameter 2: Value to be set

Let’s take the code as an example:

All Magic methods all use public. As above, we define attributes in the class, and then we create an object. The set attribute has two parameters, one is the attribute name and the other is the attribute value. We print them out in the class;

public function __set($name,$value)
    {
        var_dump($name,$value);
    }
}
$niu = new Person();
//echo $niu->pome;
$niu->pome = &#39;答案很长,我准备用一生的时间来回答,你准备要听了吗?&#39;;

Code display result:

What are magic methods in PHP? What are the commonly used magic methods?

Supplement: (Detailed explanation next time)

You can use unset externally Destroy the public properties in the object

_unset

Trigger timing: The object is called when the private or protected member properties are destroyed externally

This method has one One parameter: The parameter is the private member attribute name

_isset

Trigger timing: The object is called when externally judging private or protected member attributes,

This method has one parameter: the parameter is the private member attribute name

construct:Construction method

Triggering time: automatically called when creating the object

destruct: Destruction method

toString (understand)

Trigger timing: echo-triggers when an object

This function requires return -A string

__debugInfo (understand)

Trigger timing: var_dump--Triggers when an object

This function needs to return-an array

Recommended learning: php video tutorial

The above is the detailed content of What are magic methods in PHP? What are the commonly used magic methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn