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?

Jun 17, 2021 pm 05:44 PM
magic method

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:

1

2

3

4

5

6

7

8

9

10

<?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.

1

2

3

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 :

1

2

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;

1

2

3

4

5

6

7

8

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!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Decrypting Python metaprogramming: from basics to advanced paradigms Decrypting Python metaprogramming: from basics to advanced paradigms Feb 19, 2024 pm 03:30 PM

Python metaprogramming basics Python metaprogramming is the ability to dynamically manipulate Python code, which makes Python a very powerful language. Metaprogramming can be implemented in the following ways: Class decorator: A class decorator is a decorator that modifies the definition of a class. It can be used to add or modify the properties and methods of a class, and can also be used to control the instantiation process of a class. defadd_method_to_class(cls):defnew_method(self):print("Thisisanewmethod")setattr(cls,"new_method",new_method)returncls@a

How to follow the execution order of PHP magic methods? How to follow the execution order of PHP magic methods? Apr 17, 2024 pm 09:33 PM

The execution order of PHP magic methods follows the following rules: magic methods with high priority are executed first. If both the subclass and the parent class define magic methods with the same name, the magic method of the subclass will be executed first. If a class defines both a regular method and a magic method with the same name, the regular method will be executed first.

What is a magic method? How to use it in Laravel What is a magic method? How to use it in Laravel Sep 26, 2022 pm 08:21 PM

What is a magic method? How to use it in Laravel? The following article will introduce to you how to apply PHP magic methods in Laravel. I hope it will be helpful to you!

PHP development: Use reflection and magic methods to achieve automatic code generation and dynamic calling PHP development: Use reflection and magic methods to achieve automatic code generation and dynamic calling Jun 15, 2023 pm 04:16 PM

In PHP development, reflection and magic methods are two commonly used techniques. When we need to automatically generate code or dynamically call certain functions, reflection and magic methods can make our code more flexible and efficient. In this article, we will explore how to use reflection and magic methods to achieve automatic code generation and dynamic invocation. Reflection is a powerful tool provided by PHP, which can help us obtain information such as classes, methods, and properties when the program is running. Through reflection, we can dynamically obtain information such as methods, properties, and annotations of a class or object, so that

Take you through 16 PHP magic methods Take you through 16 PHP magic methods May 16, 2022 pm 08:45 PM

What is a magic method? This article will take you through 16 magic methods that PHP developers must know and know. I hope it will be helpful to you!

Magic methods for PHP functions Magic methods for PHP functions May 19, 2023 am 08:06 AM

PHP is a server-side scripting language developed based on C language, which is widely used in web development. Functions are one of the most basic and commonly used components in programs. PHP also provides many magic methods related to functions, which can help developers better take advantage of functions. In this article, we will introduce the magic methods of PHP functions and their usage. __construct()__construct() is one of the most commonly used magic methods in PHP. It is automatically called when creating an object for initialization.

Magic Methods: Understanding __construct, __destruct and other core methods in PHP Magic Methods: Understanding __construct, __destruct and other core methods in PHP Jun 19, 2023 pm 11:22 PM

Magic methods: Understand core methods such as __construct and __destruct in PHP. In the PHP language, there are some special methods called "magic methods", including __construct, __destruct, etc. These methods play an important role in object-oriented programming in PHP. This article will explain the role and practical application of these methods. __construct method__construct method is a very important method, it is in PHP

Uncovering the secrets of magic methods in PHP Uncovering the secrets of magic methods in PHP Oct 17, 2023 am 10:34 AM

PHP is one of the most widely used programming languages ​​for developing web applications. Its popularity stems not only from the simplicity of its syntax, but also from the flexibility it offers developers. A key feature that enables this flexibility is the concept of "magic methods" in PHP.

See all articles