PHP Object Oriented Programming (oop) Learning Notes of V PHP Namespace

  • 2021-07-01 06:45:39
  • OfStack

Namespace Overview

In PHP, namespaces are used to solve two types of problems encountered when creating reusable code, such as classes or functions, when writing class libraries or applications:

Name conflicts between user-written code and classes/functions/constants or third-party classes/functions/constants within PHP.
Create an alias (or short) name for very long identifier names (usually defined to alleviate Class 1 problems) to improve the readability of the source code.
The PHP namespace provides a way to combine related classes, functions, and constants into one. The following is an example that illustrates the syntax of PHP namespace:

Define Namespace

While any legitimate PHP code can be included in a namespace, only three types of code are affected by the namespace: classes, functions, and constants. Namespaces are declared by the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code. In addition, unlike other language features of PHP, the same namespace can be defined in multiple files, which allows the contents of the same namespace to be stored in different files. Of course, you can also define multiple namespaces in the same file.


namespace MyProject;
class MyClass
{
    #code...
}

Define child namespaces: Much like directories and files, the PHP namespace also allows you to specify hierarchical namespace names. Therefore, the names of namespaces can be defined in a hierarchical way:


namespace MyProject\helper\http;
class MyClass
{
    #code...
}

Defining multiple namespaces in the same file: There are two ways to declare multiple namespaces in the same file, but in actual programming practice, it is highly discouraged to define multiple namespaces in the same file. This method is mainly used to merge multiple PHP scripts in the same file. The first method is listed below.


namespace MyProject\helper\http;
class MyClass
{
    #code...
}
namespace MyProject\helper\request;
class MyClass
{
    #code...
}

However, this method is strongly not recommended. You can refer to the following brace definition method:


namespace MyProject\helper\http;
{
    class MyClass
    {
        #code...
    }
}
namespace MyProject\helper\request;
{
    class MyClass
    {
        #code...
    }
}

Elements in the PHP namespace use

Before we discuss how to use namespaces, we must understand how PHP knows which 1 namespace elements to use. Class names can be referenced in three ways:

Unqualified names, or class names that do not contain prefixes, such as $a=new foo (); Or foo:: staticmethod (); . If the current namespace is currentnamespace, foo will be resolved to currentnamespace\ foo. If the code that uses foo is global and does not contain code in any namespace, foo is resolved to foo. Warning: If a function or constant in a namespace is not defined, the unqualified function name or constant name is resolved to a global function name or constant name. See Using Namespaces: Backup Global Function Names/Constant Names for details.

A qualified name, or a name that contains a prefix, such as $a = new subnamespace\ foo (); Or subnamespace\ foo:: staticmethod (); . If the current namespace is currentnamespace, foo is resolved to currentnamespace\ subnamespace\ foo. If the code that uses foo is global and does not contain code in any namespace, foo is resolved to subnamespace\ foo.

A fully qualified name, or a name that contains a global prefix operator, for example, $a = new\ currentnamespace\ foo (); Or\ currentnamespace\ foo:: staticmethod (); . In this case, foo is always resolved to the literal name in the code (literal name) currentnamespace\ foo.
Use Namespace: Alias/Import

Allowing external fully qualified names to be referenced or imported by aliases is an important feature of namespaces. PHP namespaces support two ways to use aliases or import: for class names or for namespace names. In PHP, aliases are implemented through the operator use.

Note that PHP does not support importing functions or constants.


namespace foo;
use My\Full\Classname as Another;
//  The following example is the same as the  use My\Full\NSname as NSname  Same 
use My\Full\NSname;
//  Import 1 Global classes 
use \ArrayObject;

Name resolution rules

Before explaining the name resolution rules, let's look at one important definition:

Unqualified name Unqualified name: An identifier whose name does not contain a namespace delimiter, such as Foo
Qualified name Qualified name: An identifier with a namespace delimiter in the name, such as Foo\ Bar
Fully qualified name Fully qualified name: An identifier that contains a namespace delimiter in the name and begins with a namespace delimiter, for example\ Foo\ Bar. namespace\ Foo is also a fully qualified name.
Name resolution follows the following rules:

Calls to fully qualified-named functions, classes, and constants are resolved at compile time. For example, new\ A\ B resolves to the class A\ B.
All unqualified and qualified names (not fully qualified names) are converted at compile time according to the current import rules. For example, if the namespace A\ B\ C is imported as C, the call to C\ D\ e () is converted to A\ B\ C\ D\ e ().
Inside 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 () is converted to A\ B\ C\ D\ e ().
Unqualified class names are converted at compile time according to the current import rules (replacing short import names with full names). For example, if the namespace A\ B\ C is imported as C, new C () is converted to new A\ B\ C ().
Inside the namespace (for example, A\ B), function calls to unqualified names are resolved at run time. For example, the call to the function foo () is resolved as follows:
1) Find a function named A\ B\ foo () in the current namespace
2) Try to find and call the function foo () in the global (global) space.
Invokes to unqualified names or qualified name classes (not fully qualified names) within a namespace (for example A\ B) are resolved at run time. The following is the parsing procedure for calling new C () and new D\ E (): the parsing of new C ():
Find the A\ B\ C class in the current namespace.
Attempt to automatically load classes A\ B\ C.

Parsing of new D\ E ():
Prefix the class name with the current namespace name to: A\ B\ D\ E, and find the class.
Try to load the class A\ B\ D\ E automatically.

To reference global classes in the global namespace, you must use the fully qualified name new\ C ().

Example Name Resolution Sample


<?php
namespace A;
use B\D, C\E as F;
//  Function call 
foo();      //  First try to call the call defined in the namespace "A" Function in foo()
            //  Try calling the global function again  "foo"
\foo();     //  Call global space function  "foo" 
my\foo();   //  Call is defined in the namespace "A\my" Middle function  "foo" 
F();        //  First try to call the call defined in the namespace "A" Function in  "F" 
            //  Try calling the global function again  "F"
//  Class reference 
new B();    //  Create Namespace  "A"  Classes defined in  "B"  Adj. 1 Objects 
            //  If not found, try to load the class automatically  "A\B"
new D();    //  Create a namespace using import rules  "B"  Classes defined in  "D"  Adj. 1 Objects 
            //  If not found, try to load the class automatically  "B\D"
new F();    //  Create a namespace using import rules  "C"  Classes defined in  "E"  Adj. 1 Objects 
            //  If not found, try to load the class automatically  "C\E"
new \B();   //  Create a class defined in the global space  "B"  Adj. 1 Objects 
            //  If not, try to load the class automatically  "B"
new \D();   //  Create a class defined in the global space  "D"  Adj. 1 Objects 
            //  If not, try to load the class automatically  "D"
new \F();   //  Create a class defined in the global space  "F"  Adj. 1 Objects 
            //  If not, try to load the class automatically  "F"
//  Call another 1 Static methods or namespace functions in namespaces 
B\foo();    //  Call namespace  "A\B"  Middle function  "foo"
B::foo();   //  Call namespace  "A"  Classes defined in  "B"  Adj.  "foo"  Method 
            //  If a class is not found  "A\B"  Trying to load the class automatically  "A\B"
D::foo();   //  Using import rules, call the namespace  "B"  Classes defined in  "D"  Adj.  "foo"  Method 
            //  If the class  "B\D"  If not found, try to load the class automatically  "B\D"
\B\foo();   //  Call namespace  "B"  Function in  "foo" 
\B::foo();  //  Calling classes in global space  "B"  Adj.  "foo"  Method 
            //  If the class  "B"  If not found, try to load the class automatically  "B"
//  Static method or function in the current namespace 
A\B::foo();   //  Call namespace  "A\A"  Classes defined in  "B"  Adj.  "foo"  Method 
              //  If the class  "A\A\B"  If not found, try to load the class automatically  "A\A\B"
\A\B::foo();  //  Call namespace  "A\B"  Classes defined in  "B"  Adj.  "foo"  Method 
              //  If the class  "A\B"  If not found, try to load the class automatically  "A\B"
?>


Related articles: