What are the magic methods in php and what are their functions?

青灯夜游
Release: 2023-02-26 10:10:01
Original
2762 people have browsed it

In object-oriented programming, PHP provides a series of magic methods, which provide a lot of convenience for programming. Magic methods in PHP usually start with __ (two underscores) and do not require explicit calls but are triggered by certain conditions. This article briefly summarizes the magic methods available in PHP.

What are the magic methods in php and what are their functions?

__construct()

__construct() is the constructor of a class and is by far the most commonly used function. When creating an object, you can do some initialization work in the constructor. You can define any number of parameters for the constructor, as long as the corresponding number of parameters is passed in when instantiating. Any exception that occurs in the constructor prevents the object from being created.

__desctruct()

__desctruct() is the destructor of a class. It is usually called automatically when the object is destroyed or the script execution ends. The destructor does not receive any parameters. . Some cleanup work is often performed in the destructor, such as closing the database connection, etc.

__call()

__call() is called when calling a method that does not exist or is inaccessible. It receives two parameters, one is the name of the method to be called, and the other is an array containing the function parameters. We can use this method to call the function of the same name in the sub-object

__callStatic()

__callStatic() when the static method being called does not exist or has insufficient permissions will be called when. It receives two parameters, one is the name of the method to be called, and the other is an array containing the function parameters. Its usage is exactly the same as __call().

__get()

__get() will be called when we try to access a non-existent property or a non-public property. It receives a parameter, which represents the name of the accessed attribute, and returns the value of the attribute

__set()

__set() when we try to modify an unavailable It will be called when accessing the attribute. It receives two parameters, one indicating the name of the attribute and one indicating the value of the attribute.

__isset()

__isset() is called when the isset() or empty() method is called on an inaccessible property. It receives one parameter, Represents the name of the attribute. It should return a Boolean value indicating whether the property exists.

__unset()

__unset() will be called when the unset() function is called to destroy an inaccessible property. It receives a parameter, expressing The name of the property.

__sleep()

__sleep() will be called when serializing an object (calling serialize()). It takes no parameters and should return an array containing all properties that should be serialized. Within this magic method, you can also perform some other operations.

One thing to note is that do not perform any destruction operations in this function, because this may affect the running object

__wakeup()

__wakeup() will be called when deserializing the stored object. It does not receive any parameters and does not return any value. You can use it to handle database connections or resources lost during serialization

__toString()

__toString() when we use objects as strings will be called, it does not receive any parameters. This method allows us to define the representation of the object.

__invoke()

The response method when calling an object by calling a function

__set_state()

This static method will be called when calling var_export() to export a class.

__clone()

__clone() can solve the above problem. This magic method is called when the clone keyword is used on an object.

The above is the detailed content of What are the magic methods in php and what are their functions?. 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