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.
__construct()
The constructor 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() (Recommended learning: PHP programming from entry to proficiency)
The destructor is usually called when the object is destroyed. The constructor does not receive any parameters. Some cleanup work is often performed in the destructor, such as closing the database connection, etc.
__get()
is called when we try to access a property that does not exist. It receives a parameter, which represents the name of the accessed attribute, and returns the value of the attribute
__set()
The magic method is when we try to modify It will be called when an inaccessible property is present. It receives two parameters, one representing the name of the property and one representing the value of the property.
__isset()
The magic method is called when the isset() method is called on an inaccessible property. It receives a parameter representing the name of the property. It should return a Boolean value indicating whether the property exists.
__unset()
The magic method will be called when the unset() function is called to destroy an inaccessible attribute. It receives a parameter expressing the name of the attribute.
__toString()
will be called when we use the object as a string. It does not receive any parameters. This method allows us to define the representation of the object.
__clone()
The magic method __clone() can solve the above problem. This magic method is called when the clone keyword is used on an object.
__sleep()
The magic method __sleep() is 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()
The magic method __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
__call()
Magic method __call() when calling non-existent or inaccessible method will be called. 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
The above is the detailed content of What is the use of magic methods in php. For more information, please follow other related articles on the PHP Chinese website!