In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the meaning of php underlining refers to. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
An underscore in php represents private variables and private methods, and two underscores are built-in variables in PHP. For example, php stipulates that methods that begin with two underscores are reserved as magic methods, so it is recommended that function names should not start with _ _, unless it is to overload existing magic methods.
This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer
What does php underscore mean?
The meaning of variables beginning with an underscore in PHP
Naming rules
Add a private
The addition of two is generally default and predefined by the system, that is, the so-called:
=
Magic method and Magic constant
=
A constant that starts and ends with a double underscore in ★ PHP is a "magic constant":
The current line number in the _ LINE__ file.
The full path and file name of the _ _ FILE__ file.
The directory where the _ _ DIR__ file is located. If used in an included file, the directory where the included file is located is returned. It is equivalent to dirname (_ _ FILE__). Unless it is the root directory, the directory name does not include the trailing slash
Note: the above content comes from "PHP Chinese Manual-> language reference-> constant-> Magic constant".
From later versions of php5, php's classes can use magic methods.
Php stipulates that methods that begin with two underscores (_ _) are reserved as magic methods, so it is recommended that function names not start with _ _, unless it is to overload existing magic methods.
The magic methods in PHP are: _ _ construct, _ _ destruct, _ _ call, _ _ callStatic,__get, _ _ set, _ _ isset, _ _ unset, _ _ sleep, _ _ wakeup, _ _ toString, _ _ set_state, _ _ clone, _ _ autoload
1 、 _ _ get 、 _ _ set
These two methods are designed for properties that are not declared in the class and their parent class
_ _ get ($property) this method is triggered when an undefined property is called, passing the name of the property being accessed
This method is triggered when _ _ set ($property, $value) assigns a value to an undefined property, passing the property name and value that is set.
The non-declaration here includes attributes whose access control is proteced,private (that is, properties that do not have permission to access) when using object calls.
2 、 _ _ isset 、 _ _ unset
_ _ isset ($property) call this method when the isset () function is called on an undefined property
_ _ unset ($property) call this method when the unset () function is called on an undefined property
Like the _ _ get and _ _ set methods, the non-declaration here includes properties whose access control is proteced,private (that is, properties that do not have permission to access) when called with an object.
3 、 _ _ call
_ _ call ($method, $arg_array) this method is called when calling an undefined method
The undefined methods here include methods that do not have permission to access; if the method does not exist, look for the method in the parent class, and call the _ _ call () side of the class if it does not exist either. Method, if the _ _ call () method does not exist in this class, go to the _ _ call () method in the parent class
4 、 _ _ autoload
The _ _ autoload function, which is called automatically when you try to use a class that has not yet been defined. By calling this function, the script engine has one last chance to load the required classes before the PHP error fails.
If you want to define a global autoload class, you must register the processing class with the PHP standard library with the spl_autoload_register () method:
Note: the exception thrown in the _ _ autoload function cannot be caught by the catch statement block and causes a fatal error, so it should be caught in the function itself.
5 、 _ _ construct 、 _ _ destruct
The _ _ construct constructor, which is called when an object is created, has the advantage of giving the constructor a unique name, regardless of the name of the class in which it resides, compared to PHP4. So that when you change the name of the class, you don't need to change the name of the constructor.
The _ _ destruct destructor method, which PHP will call before the object is destroyed (that is, before it is cleared from memory). By default, PHP only frees up memory occupied by object properties and destroys object-related resources, and destructors allow you to execute arbitrary code to clear memory after using an object. When PHP decides that your script is no longer related to objects, the destructor will be called.
Within the namespace of a function, this happens when the function return.
For global variables, this occurs at the end of the script.
If you want to destroy an object explicitly, you can assign any other value to the variable pointing to the object. Variables are usually assigned to NULL or called unset.
6 、 _ _ clone
The object assignment in PHP5 is the reference assignment used. If you want to copy an object, you need to use the clone method. When calling this method, the object will automatically call the _ _ clone magic method. If you need to perform some initialization operations in object replication, you can do so in the _ _ clone method.
7 、 _ _ toString
The _ _ toString method is called automatically when an object is converted to a string, such as when printing an object using echo.
If the class does not implement this method, the object cannot be printed through echo, otherwise it will display: Catchable fatal error: Object of class test could not be converted to string in
This method must return a string.
Prior to PHP 5.2.0, the _ _ toString method would only take effect if used in combination with echo () or print (). After PHP 5.2.0, it can take effect in any string environment (for example, through printf (), using the% s modifier), but not in a non-string environment (such as using the% d modifier). From PHP 5.2.0, if you convert an object that does not have a _ _ toString method defined to a string, an E_RECOVERABLE_ERROR error is reported.
8 、 _ _ sleep 、 _ _ wakeup
Used when _ _ sleep is serialized
Called when _ _ wakeup is acting in a line
Serialize () checks to see if there is a function in the class with the magic name _ _ sleep. If so, the function will run before any serialization. It clears the object and should return an array of all variable names that should be serialized in the object.
The purpose of using _ _ sleep is to close any database connections that objects may have, submit waiting data, or perform similar cleanup tasks. In addition, this function is useful if you have very large objects that do not need to be fully stored.
Instead, unserialize () checks for the existence of a function with the magic name _ _ wakeup. If it exists, this function can reconstruct any resources that the object may have.
The purpose of using _ _ wakeup is to rebuild any database connections that may be lost during serialization and to handle other reinitialization tasks.
9 、 _ _ set_state
This static method is called when var_export () is called (valid since PHP 5.1.0).
The only parameter to this method is an array containing the following array ('property' = > value,...) The class properties of the format arrangement.
10 、 _ _ invoke
When you try to call an object as a function call, the _ _ invoke method is called automatically.
PHP5.3.0 and above are valid
11 、 _ _ callStatic
It works like the _ _ call () magic method, and _ _ callStatic () is used to handle static method calls
PHP5.3.0 and above are valid
PHP does enhance the definition of the _ _ callStatic () method; it must be public and must be declared static. Similarly, the _ _ call () magic method must be defined as public, as must all other magic methods.
This is the end of the article on "what does php underline mean?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.