Detailed explanation and application of PHP namespace

*文
Release: 2023-03-18 11:40:01
Original
2111 people have browsed it

Many novices may not understand namespaces or how to apply namespaces. This article will describe the use of PHP namespaces. I hope it will be helpful to everyone.

1. What is the namespace in PHP?

What is a namespace? "Broadly speaking, a namespace is a way of encapsulating things. This abstract concept can be seen in many places. For example, in operating systems, directories are used to group related files. For files in a directory, It plays the role of a namespace. For example, the file foo.txt can exist in the directories /home/greg and /home/other, but two foo.txt files cannot exist in the same directory. , when accessing the foo.txt file outside the directory /home/greg, we must put the directory name and directory separator before the file name to get /home/greg/foo.txt. This principle is applied to the field of programming as namespace. Concept."——Namespace overview

2. How to understand PHP namespace?

Essentially, a namespace is a container. We can put classes, functions and variables into this container, and they can access each other unconditionally in the same namespace. Outside the namespace, other namespaces must be referenced or imported in order to call the items they contain.

The concept of namespace is the same as that of file directories in the shell. All files in the current directory can be accessed directly by file name. If you need to access files in other directories, you need to enter a relative path or an absolute path.

Reference method:

namespace foo;
 class Foo {   
         public function foo()   
             {        
                  return \top\namespace\bar\Bar::fuck();    
              }
             }
Copy after login


Import method:

namespace foo; 
use top\namespace\bar\Bar; 
 class Foo {
        public function foo() 
            {        return Bar::fuck();  
            }
           }
Copy after login

Importing is equivalent to copying the target class into the current namespace .

3. What are the practical applications of PHP namespaces?

The namespace exists to solve the following two problems:

1). Between the code written by the user and the classes/functions/constants inside PHP or third-party classes/functions/constants name 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 the 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 namespace without "reference" or "import". They belong to the same family. Any subnamespace is another namespace, another container, without any special relationship other than that between containers.

3. Laravel uses classmap 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 in /vendor/composer/autoload_classmap.php, which will be recompiled and generated every time composer dump-autoload is run.


Related recommendations:

Summary of PHP automatic loading and namespace application

Introduction to PHP namespaces, traits and generators

2017 Essential Learning Framework for Newbies: Thinkphp5 Framework Tutorial: Detailed explanation of namespace


#

The above is the detailed content of Detailed explanation and application of PHP namespace. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template