In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the namespace in PHP? I believe many inexperienced people don't know what to do about it. This article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
How namespaces exist in the global space in PHP
When a namespace is defined in the file, that is, namesapce specifies the current namespace, when calling global classes, functions, and constants, you need to add a "\", that is, a fully qualified access symbol to indicate that the class, function, and constant are global, not in the current namespace. Especially if the current namespace contains content with the same name as global classes, functions, and constants.
Namespace FILE6
Function show ()
{
Echo strtoupper ('aaa'), PHP_EOL; / / call your own
Echo\ strtoupper ('aaa'), PHP_EOL; / / calls the global
}
Function strtoupper ($str)
{
Return _ _ NAMESPACE__. ':. \ strtoupper ($str)
}
In this FILE6 namespace, we define a strtoupper () method. As mentioned between, namespaces are created to solve the problem of the same name, and this method is exactly the same as the one that comes with the global php. So, which method we need to call when calling. What if this method is not defined in the current namespace? Don't worry, that's what we're going to talk about.
Backup global function / constant
From the previous example, we can see the role of the global fully qualified accessor. When the global character is not used, the strtoupper () method first calls the method in the current namespace. Then the role of backup is to find the relevant function globally if it is not found in the current namespace. The definition in the document is as follows:
When PHP encounters an unqualified class, function, or constant name, it uses a different precedence policy to resolve the name. Class names always resolve to names in the current namespace. Therefore, fully qualified names must be used when accessing class names within the system or not included in the namespace. For functions and constants, if the function or constant does not exist in the current namespace, PHP will fall back and use the function or constant in the global space.
That is to say, functions and constants will have the ability to look globally. But the class is not good! If you want to use a global class, be sure to add a global full qualifier. Let's look at it through an example:
Namespace FILE7
/ / the class must use a fully qualified global space
$o1 = new\ stdClass ()
/ / $O2 = new stdClass (); / / Fatal error: Uncaught Error: Class' FILE7\ stdClass' not found
/ / the method will be searched in this namespace first, and if it is not found, it will be found globally.
Function strlen ($str)
{
Return _ _ NAMESPACE__. ':. (\ strlen ($str)-1)
}
Echo strlen ('abc'), PHP_EOL; / / FILE7:2, current namespace
Echo\ strlen ('abc'), PHP_EOL; / / 3, global
Echo strtoupper ('abc'), PHP_EOL; / / ABC, global
/ / constants also have backup capabilities.
Const E_ERROR = 22
Echo E_ERROR, PHP_EOL; / / 22, current namespace
Echo\ E_ERROR, PHP_EOL; / / 1, global
Echo INI_ALL, PHP_EOL; / / 7, global
Name resolution rules resolve calls to fully qualified functions, classes, and constants at compile time. For example, new\ A\ B resolves to class A\ B. All unqualified and qualified names (non-fully qualified names) are converted at compile time according to the current import rules. For example, if the namespace A\ B\ C is imported to C, the call to C\ D\ e () is converted to A\ B\ C\ D\ e (). Within the namespace, all qualified names that are not converted according to the import rules are preceded by the current namespace name. For example, if C\ D\ e () is called inside the namespace A\ B, C\ D\ e () will be converted to A\ B\ C\ D\ e (). Unqualified class names are converted at compile time according to the current import rules (using the full name instead of the short import name). For example, if namespace A\ B\ C is imported to C, new C () is converted to new A\ B\ C (). Within the namespace (for example, A\ B), function calls to unqualified names are resolved at run time. For example, the call to the function foo () parses like this: look for a function named A\ B\ foo () in the current namespace and try to find and call the function foo () in the global (global) space. Calls to unqualified names or qualified name classes (non-fully qualified names) within the namespace (for example, A\ B) are resolved at run time. The following is the parsing process of calling new C () and new D\ E (): parsing new C (): looking for class A\ B\ C in the current namespace. Attempt to automate class A\ B\ C.
Resolution of new D\ E ():
Add the current namespace name before the class name to: a\ B\ D\ E, and then look for the class. Try automounting class A\ B\ D\ E.
In order to reference a global class in the global namespace, you must use the fully qualified name new\ C ().
After reading the above, have you mastered the method of what namespaces are in PHP? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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
Step 1: declare: `step 2: TouchAction ta = new TouchAction (drive)
© 2024 shulou.com SLNews company. All rights reserved.