search
HomeBackend DevelopmentPHP ProblemWhat are magic methods in PHP? What are the commonly used magic methods?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version