


What 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.
#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 |
|
The code displays the result:
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 |
|
Then print out $name,
The code displays the result:
So we can use the if statement to judge through the above code :
1 2 |
|
The code displays the result:
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 |
|
Code display result:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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? The following article will introduce to you how to apply PHP magic methods in Laravel. I hope it will be helpful to you!

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

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!

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: 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

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.
