1. What is namespace in PHP?
2. How to understand PHP namespace?
namespace foo; class Foo { public function foo() { return \top\namespace\bar\Bar::fuck(); } }
Import method:
namespace foo; use top\namespace\bar\Bar; class Foo { public function foo() { return Bar::fuck(); } }
Importing is equivalent to copying the target class into the current namespace
Namespace exists to solve the following two problems:
1. Names between user-written code and PHP internal classes/functions/constants or third-party classes/functions/constants Conflict.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) to improve the readability of the source code
4. Some tips
1. Classes in the same space call each other directly and belong to the same family. For example, in the PageController class in Laravel, you can directly write code like Page::all() to call
Page model because both of them are under the top-level namespace.
2. If a class exists in a non-top-level namespace, it can only call other classes in the same current namespace without "referencing" or "importing". They belong to One family. Any subnamespace is another namespace, another container, without any special relationship other than that between containers.
3. Laravel uses classmap method for automatic loading (autoload). Although PHP has the advanced feature of namespace, this is only a logical relationship, and the require file is still required. The corresponding relationship between this class and the file exists /vendor/composer/autoload_classmap.php , composer dump-autoload will be recompiled and generated every time. Reprinted from: http://lvwenhan.com/php/401.html
The above has introduced the introduction to PHP namespaces, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.