In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use eight magic constants commonly used in PHP", in daily operation, I believe many people have doubts on how to use eight magic constants commonly used in PHP, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "how to use eight magic constants commonly used in PHP" doubts helpful! Next, please follow the small series to learn together!
What are magic constants?
Magic constants are not exactly constants. Constants, as we saw earlier in this article, cannot be changed once they are defined. But magic constants are PHP's predefined constants that can change depending on where they are located.
Many predefined constants are provided by PHP in any script, and their extensions vary according to the constants. Magic constants can be understood as special predefined constants.
Magic constants are usually written with two underscores (__) beginning and ending with two underscores, such as__LINE__,__FILE__, etc. When it comes to writing formats, remember that magic constants are case-insensitive, but try to write them in upper case.
Use of Magic Constants
PHP provides eight magic constants in the runtime environment. As mentioned above, although they are called constants, they are not exactly constants. Their values change with changes in the code. Let's look at these eight magic variables separately.
1.__ LINE__indicates the current line number in the file
Let's take a look at the use of the__LINE__magic constant and see how the value of the magic constant changes with position.
Examples are as follows:
Output:
In the above example, we can get the number of lines at the current code location through the magic variable__LINE__.
One thing to note is that we can see that when the code is located in different locations, the output results are also different. So we said above that magic constants are not constants, and their values change with code. Let's look down:
2.__ FILE__indicates the absolute path of the file
The absolute path to the file can be obtained through__FILE__, and this path contains the file name. Note that if the magic constant is used in an included file, the returned result is the included file name.
Let's take a look at the use of__FILE__by example, as follows:
Output:
In the above example we are through__FILE__to complete the absolute path to the file.
3.__ DIR__is used to indicate the directory where the file is located
The directory where the file is located can be obtained through__DIR__. The directory where the file is located is different from the absolute path where the file is obtained by__FILE__. The directory where the file is located does not have the file name of the current file. This magic constant can also be interpreted as dirname(__FILE__).
Let's take a look at the use of the__DIR__magic constant through an example, as follows:
Output:
In the above example, we get the directory where the current file is located in two ways, one is through the__DIR__magic constant, and the other is dirname(__FILE__), both of which can achieve the same effect.
4.__ FUNCTION__Used to get the current function name or method name
__FUNCTION__can get the name of the current function or method. Next, let's take a look at the usage of__FUNCTION__magic constant through an example.
Examples are as follows:
Output:
From the above example we know that__FUNCTION__can get the function name of the current function.
5.__ CLASS__Used to get the current class name
__CLASS__can be used to obtain the current class name, and the total number of Qi also includes the scope or namespace of the current class. Let's take a look at the use of__CLASS__by example.
Examples are as follows:
Output:
As you can see from the above example, you can get the current class name in PHP by__CLASS__.
6.__ TRAIT__Current trait name
__TEAIT__can represent the current trait name, which includes the region or namespace of the trait. Let's take a look at the use of__TRAIT__through an example.
Examples are as follows:
Output:
7.__ METHOD__Current method name
__METHOD__can get the current method name, which also includes the class name. Next, let's take a look at the use method of__METHOD__through an example. The example is as follows:
Output:
In the example above, we obtained the function name and method name via__METHOD__. The method name includes the class name.
8.__ NAMESPACE__The namespace name of the current file
__NAMESPACE__can get the namespace name of the current file. Next, let's take a look at the following examples:
Output:
At this point, the study of "how to use eight magic constants commonly used in PHP" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.