Home > Backend Development > PHP Problem > 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?

慕斯
Release: 2023-03-10 17:16:01
Original
2773 people have browsed it

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;
?>
Copy after login

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;
    }
Copy after login

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;
Copy after login

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;;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template