In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to quickly understand the magic method in PHP". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to quickly understand the magic method in PHP".
In our previous article, when we introduced the _ _ clone () method, we mentioned that this is a kind of magic method. The constructors and destructors also mentioned in "taking you to distinguish between constructors and destructors in a class" are also called constructors and destructors, and they are also a kind of magic methods. So what is magic? What are the common methods of it? How does it work? Next, let's take a look.
PHP magic method
In PHP object-oriented programming, there are a series of magic methods in PHP. If you take a look at the _ _ clone\ _ _ construct\ _ _ destruct we learned before, we can find one thing in common, that is, they all start with two underscores and are called automatically under certain conditions.
Next, let's take a look at some commonly used magic methods, combined with examples to see how to use them.
_ _ set () method
When the _ _ set () method is used to assign values to undefined or invisible class attributes in the current environment, it should be noted that all magic methods are called automatically under certain conditions, and assigning values to undefined or invisible class attributes in the current environment is a specific condition for the call of the _ _ set () method.
The syntax format is as follows:
Public function _ _ set (the variable to operate, the value of the variable to be manipulated) {...;}
Examples are as follows:
Output result:
As you can see from the above example, the _ _ set () method is called when an undefined or invisible class attribute is assigned in the current environment.
_ _ get () method
The specific condition for automatically calling the _ _ get () method is that when calling or getting class properties that are not defined or invisible in the current environment, the syntax format for defining the method is as follows:
Public function _ _ get ($name) {...;}
The parameter $name represents the name of the variable to be operated. Let's take a look at the use of the _ _ get () method through an example, as shown below:
Output result:
From the above results, you can see that the _ _ get () method is automatically called when calling or getting class attributes that are not defined or visible in the current environment.
_ _ isset () method
The specific condition for automatically calling the _ _ isset () method is that when the isset () or empty () function is used outside the class for properties that are not accessible or do not exist in the class, the syntax format of the method is as follows:
Public function _ _ isset ($name) {...;}
One of the things we need to pay attention to is:
The parameter $name is the name of the attribute to be accessed.
The isset () function is used to check whether a variable exists and to determine that it is not NULL, and if the passed variable exists, it returns true, and there is no return false.
The empty () function is used to check whether a variable is empty, and if the value of the variable is equal to FALSE, then the variable is considered not to exist.
If we want to use the isset () or empty () function to detect private attributes in the class, we just need to add a _ _ isset () method to the class, and the _ _ isset () method in the class will be automatically called when the isset () or empty () function is used outside the class.
Next, let's take a look at the use of the _ _ isset () method through an example, as follows:
Output result:
It is important to note that the property_exists () function can be used to detect whether the attribute is defined in the class, and the syntax format is
Property_exists ($class_name,$property_name)
Where $class_name is the class name in the form of a string, that is, to determine whether the property_name attribute is defined in the class class_name.
_ _ unset () method
The specific condition for automatically calling the _ _ unset () method is that when the unset () function is used outside the class for properties that are not accessible or do not exist in the class, the syntax format of the method is as follows:
Public function _ _ unset ($name) {...;}
One of the things we need to pay attention to is:
The parameter $name is the name of the attribute to be accessed.
The unset () function deletes the specified variable, passing in one or more variables as arguments, and the function does not return a value.
You can use the unset () function to remove member attributes from a class outside the class. If you want to delete the public attribute in the class, you can simply use the unset () function
If you want to remove private attributes from a class, you need to add a _ _ unset () method to the class.
Next, let's take a look at the use of the _ _ unset () method through an example, as follows:
Output result:
You can see from the above example that the _ _ unset () method is called automatically when the unset () function is used outside the class for attributes that are not accessible or do not exist in the class.
In the above examples, we introduce some commonly used magic methods, but this is not all of the magic methods, so let's give you some magic methods:
_ _ call ()-automatically called when a method that does not exist is called
_ _ sleep ()-called automatically when serialization using serialize
_ _ toString ()-automatically called when an object is converted to a string
_ _ invoke ()-called automatically when you try to call an object as a method call
_ _ wakeup ()-called automatically when deserialization using unserialize is used
_ _ callStatic ()-automatically called when a static method that does not exist is called
_ _ debugInfo ()-automatically called when printing object information using var_dump ()
_ _ set_state ()-automatically called when the var_export () function is used, accepting an array parameter
Thank you for your reading, the above is the content of "how to quickly understand the magic methods in PHP". After the study of this article, I believe you have a deeper understanding of how to quickly understand the magic methods in PHP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.