Home  >  Article  >  Backend Development  >  How to use the use keyword in php namespace

How to use the use keyword in php namespace

青灯夜游
青灯夜游Original
2021-09-30 17:05:173725browse

In PHP, the use keyword can be used to import a namespace, the syntax "use namespace;"; it can also be used to import functions and constants, and set aliases for them, the syntax "use namespace as alias;" .

How to use the use keyword in php namespace

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

What is a namespace ?

In a broad sense, namespace is a way of encapsulating things, and this abstract concept can be seen in many places. For example, directories are used in operating systems to group related files, and they act as namespaces for the files in the directory.

Definition of namespace

The namespace in PHP was added in PHP5.3, if you know C , then namespaces are nothing new. However, namespaces are still very important in PHP.

PHP namespace can solve the following two types of problems:

  • The difference between user-written code and PHP internal classes/functions/constants or third-party classes/functions/constants Naming conflicts between;

  • Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) to improve the source Code readability.

Define a namespace (using the keyword namespace)

Although any legal PHP code can be included in a namespace, only classes Code of types such as (including abstract classes and traits), interfaces, functions, and constants are affected by namespaces.

The definition of a namespace needs to be declared through the keyword namespace. The syntax format is as follows:

namespace 命名空间名;

[Example] Let’s demonstrate how to define a namespace:

<?php
    namespace MyProject;    // 定义名为 MyProject 的命名空间。
    const CONNECT_OK = 1;
    class Myclass {
        /* ... */
    }
    function Myfunc() {
        /* ... */
    }
?>

Except for the declare statement used to define the source file encoding before declaring the namespace, all non-PHP code (including whitespace characters) cannot appear before the namespace declaration.

Use namespace: alias/import

PHP allows the use of external namespaces through alias references or imports. This is naming an important feature of space. This is somewhat similar to how you can create symbolic links to other files or directories in a Unix-like file system.

Use the use keyword to implement namespace import. Starting from PHP5.6, functions and constants are allowed to be imported and aliases are set for them. The syntax format is as follows:

use namespace;

In PHP, aliases are implemented through the operators use and as. The syntax format is as follows:

use 命名空间 as 别名;

[Example] Use the use operator to import and use aliases.

<?php
    namespace foo;
    use My\Full\Classname as Another;
    // 下面的例子与 use My\Full\NSname as NSname 相同
    use My\Full\NSname;
    // 导入一个全局类
    use ArrayObject;
    // 导入一个函数
    use function My\Full\functionName;
    // 导入一个函数并定义别名
    use function My\Full\functionName as func;
    // 导入一个常量
    use const My\Full\CONSTANT;
    $obj = new namespace\Another;   // 实例化 foo\Another 对象
    $obj = new Another;             // 实例化 My\Full\Classname 对象
    NSname\subns\func();            // 调用 My\Full\NSname\subns\func 函数
    $a = new ArrayObject(array(1)); // 实例化 ArrayObject 对象
                                    // 如果不使用 "use \ArrayObject" ,则实例化一个 foo\ArrayObject 对象
    func();                         // 调用 My\Full\functionName 函数
    echo CONSTANT;                  // 打印 My\Full\CONSTANT 常量
?>

Note: For names in namespaces (fully qualified names that include namespace separators, such as Foo\Bar, and relative global names that do not include namespace separators, such as FooBar), Leading backslashes are unnecessary and not recommended because imported names must be fully qualified and will not be resolved relative to the current namespace.

In order to simplify the operation, PHP also supports importing multiple namespaces in one line, separated by using,. The sample code is as follows:

<?php
    use My\Full\Classname as Another, My\Full\NSname;
    $obj = new Another;     // 实例化 My\Full\Classname 对象
    NSname\subns\func();    // 调用 My\Full\NSname\subns\func 函数
?>

The import operation is compiled and executed, but the dynamic class Names, function names, or constant names are not.

<?php
    use My\Full\Classname as Another, My\Full\NSname;
    $obj = new Another; // 实例化一个 My\Full\Classname 对象
    $a = &#39;Another&#39;;
    $obj = new $a;      // 实际化一个 Another 对象
?>

In addition, the import operation only affects unqualified names and qualified names. Fully qualified names are not affected by imports because they are deterministic.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use the use keyword in php namespace. For more information, please follow other related articles on the PHP Chinese website!

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