Home  >  Article  >  Backend Development  >  Getting started with PHP namespaces

Getting started with PHP namespaces

WBOY
WBOYOriginal
2016-08-08 09:29:41905browse

1. What is 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 Grouping related files, it plays the role of a namespace for files in a directory. For example, files. foo.txt can exist in both the directories /home/greg and /home/other, but two foo.txt files cannot exist in the same directory. Additionally, 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. The application of this principle to the field of programming is the concept of namespace. ”——Namespace Overview

2. How to understand PHP namespace?

Essentially, a namespace is a container. In this container we can put classes, functions and Variables can access each other unconditionally within the same namespace. Outside the namespace, other namespaces must be referenced or imported to call the items they contain in the namespace and the file directory in the shell. The concept is the same. You can directly access all files in the current directory. If you need to access files in other directories, you need to enter the relative path or absolute path:

.

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 3. What are the practical applications of PHP namespaces?

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

The currently very popular Composer is a namespace-based package manager/dependency manager. Similarly, Laravel's success today is largely due to the popularity of PHP5.3, which happened at the right time. Download various packages from https://packagist.org/ composer package, similar to yum, npm or gem.


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.

Statement:
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