php namespace learning details

  • 2021-01-19 22:03:35
  • OfStack

1. What is a namespace?
A namespace is a special scope that contains identifiers that fall within the scope and is itself a type of identifier. You can correspond a namespace to an operating system directory. A namespace is equivalent to a directory. Classes, functions, and constants in the namespace are equivalent to files in the directory. The same file name in the same directory (namespace) cannot be the same, but different directories can have the same name of the file.
2. What problems are namespaces used to solve?
Resolve name conflicts, such as defining a class that happens to have the same name as a class inside PHP or a class library included in include.
To improve code readability, namespaces have a single name feature, which allows you to give a class name that is more than 10 characters long a single name, thereby shortening the code without worrying about naming conflicts with other Spaces.
3. Which code is affected by namespaces?
Class: Class, Function, Constant. Only brother 3 is affected, and the rest are doing what they should. In terms of constants, after php 5.3, constants can be defined using the const keyword. Before 5.3, define was used. Namespaces are only valid for const keywords.
4. How to define a namespace


namespace MyProject;
const CONNECT_OK = 1;//php5.3 after 
class Connection { /* ... */ }
function connect() { /* ... */  }
# example 2
namespace MyProjectSubLevel;
const CONNECT_OK = 1;//php5.3 after 
class Connection { /* ... */ }
function connect() { /* ... */  }

Use the 'namespace space name' to declare a space. Before namespace, there must be no php statements other than declare statements, and there must be no non-php code, not even Spaces.
The following is an incorrect form:

$a = 1;
namespace MyProject;
?>www.ofstack.com
//Fatal error: Namespace declaration statement has to be the very first statement in the script...

In addition, the same namespace can be defined in multiple files, which is very useful for organizing frameworks. That is, with the same 1 namespace MyProject; The file at the beginning of the file is in the same namespace. So be careful not to have the same class/function/constant name between files.
It is also possible to define multiple namespaces within the same file, but this is highly discouraged. Learn how to define multiple namespaces in the same file.
5. How to use namespaces
Namespaces can be used in three ways:
. Unqualified name -- Use class/function/constant names without any separators, e.g., new Foo(); foo(); echo FOO; When a file uses a namespace,

<?php
namespace MyObject;
new Foo(); //  call MyObjectFoo();
foo(); // call MyObjectFoo();
echo FOO; // call MyObjectFOO;     

Infully qualified names -- do not start with a separator, such as new SubFoo(); This form is similar to the unqualified name form.

<?php
namespace MyObject; new SubFoo(); // call MyObjectSubFoo();

Fully qualified name - begins with a divider and is equivalent to an absolute address in the operating system. Such as new OtherNSFoo ();

<?php
namespace MyObject; new OtherNSFoo(); // call OtherNsFoo();  No matter MyObject Namespaces. 

Tip: There is a special place for functions and constants (backup global functions/constants).

<?php
namespace MyObject;
funcname(); // if MyObjectFuncname Call when there is MyObjectFuncname() Otherwise, try calling funcname(); echo FOO; // Same as above. 

There is also a special aspect to classes.

<?php
namespace MyObject;
new Foo(); //* if MyObjectFoo If it exists, call it. If it doesn't exist, call it __autoload Try to load the MyObjectFoo In class. 
// Note that global scoped classes are not automatically invoked for classes. 

As mentioned earlier, namespaces also serve one purpose - aliasing.

namespace MyObject;
use OtherNSSub as Other;
use OtherNSSub2; // The equivalent of use OtherNSSub2 as Sub2;
use /MyClass;
new Foo(); // call MyObjectFoo();
new OtherFoo(); // call  OtherNSSubFoo();
new Sub2Foo(); // call OtherNSSub2Foo();
new MyClass(); // call MyClass();

Dynamic namespaces
Dynamics can be confusing, yet flexible. Namespaces can also use dynamic language characteristics, but note that since direct invocation namespaces are compile-time resolved, dynamic characteristics are not. So 1 has to be prefixed. Such as:

namespace MyObjectSub;
new Foo(); // call  MyObjectSubFoo(),  This has been resolved at compile time to MyObjectSubFoo
$a = 'Foo';
new $a(); // Call is Foo() Rather than MyObjectSubFoo()
$b = 'MyObjectSubFoo'; // Is equivalent to  MyObjectSubFoo
new $b(); // call MyObjectSubFoo()
// If you use double quotes, use \ , such as  $a = "\MyObject\Sub";

Attachment 1: Multiple namespaces are defined in the same file
There are two ways to do this:


namespace MyProject;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
namespace AnotherProject;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }

Method 1, keep a journal.


$a = 1;
namespace MyProject;
?>www.ofstack.com
//Fatal error: Namespace declaration statement has to be the very first statement in the script...
0

In method 2, we use curly braces to enclose the code in the same namespace. This approach requires that there be no code outside curly braces other than declare. Globally scoped code is enclosed in curly braces with no spatial name.


Related articles: