Home > Backend Development > PHP Tutorial > Detailed explanation of php namespace learning_PHP tutorial

Detailed explanation of php namespace learning_PHP tutorial

WBOY
Release: 2016-07-13 10:36:54
Original
737 people have browsed it

1. What is a namespace?
A namespace is a special scope that contains identifiers within that scope and is itself an identifier. You can map the namespace to the directory of the operating system. A namespace is equivalent to a directory, and classes, functions, and constants in the namespace are equivalent to files in the directory. File names in the same directory (namespace) cannot be the same, but files with the same name can be in different directories.
2. What problem is used to solve the problem of using namespace?
Resolve name conflicts, such as defining a class that has the same name as a class inside PHP or a class in an included class library.
Improving code readability, the namespace has an alias function, which can help you give an alias to a class name that is more than ten characters long, thereby shortening the code and eliminating the need to worry about naming conflicts with other spaces.
3. Which codes will be affected by namespaces.
Three categories: classes, functions, and constants. Only the three brothers were affected, so what else should the others do? Speaking of constants, you can use the const keyword to define constants after PHP 5.3. Before 5.3, you used define. The namespace is only valid for the const keyword.
4. How to define namespace

Copy code The code is as follows:

namespace MyProject;
const CONNECT_OK = 1;//After php5.3
class Connection { /* ... */ }
function connect() { /* ... */ }
#Example 2
namespace MyProjectSubLevel;
const CONNECT_OK = 1;//php5.3 and later
class Connection { /* ... */ }
function connect() { /* ... */ }

Use `namespace space name` to declare a space. There cannot be any other php statements except the declare statement before the namespace. At the same time, there cannot be any non-php code, not even spaces.
The following is the wrong form:
Copy codeThe code is as follows:

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

Another same name Spaces can be defined in multiple files, which is very useful for organizing frameworks. That is, files starting with the same namespace MyProject;, they are the same namespace. So be careful not to have the same class/function/constant names between files.
Of course, the same file can also define multiple namespaces, but this is highly discouraged. (Understand that the same file defines multiple namespaces)
5. How to use namespaces
There are three forms of use of namespaces:
. Unqualified names--without using any separators, directly use class/ Function/constant name, such as: new Foo(); foo(); echo FOO; When the file uses a namespace,
Copy code The code is as follows:

namespace MyObject;
new Foo(); // Call MyObjectFoo();
foo(); // Call MyObjectFoo();
echo FOO; //Call MyObjectFOO;

Non-fully qualified name - does not start with a delimiter, such as new SubFoo(); This form is the same as the unqualified name.
Copy code The code is as follows:

namespace MyObject; new SubFoo(); // call MyObjectSubFoo();

Fully qualified name - starting with a delimiter, equivalent to the absolute address in the operating system. Such as new OtherNSFoo();
Copy code The code is as follows:

namespace MyObject; new OtherNSFoo (); //Call OtherNsFoo(); regardless of the MyObject namespace.

Tip: There is a special place for functions and constants (fallback global functions/constants). / /If MyObjectFuncname exists, call MyObjectFuncname(), otherwise try to call funcname(); echo FOO; //Same as above.
There is also a special place for classes.
Copy code

The code is as follows:


namespace MyObject;
new Foo(); //*If MyObjectFoo exists, call it. If it does not exist, call __autoload to try to load the MyObjectFoo class.
//Note that classes in the global scope will not be automatically called.

As mentioned before, namespaces have another purpose - to create aliases. / /Equivalent to use OtherNSSub2 as Sub2;
use /MyClass;
new Foo(); //Call MyObjectFoo();new OtherFoo(); //Call OtherNSSubFoo();new Sub2Foo (); //Call OtherNSSub2Foo();new MyClass(); //Call MyClass();
6. Dynamic namespace
Dynamics are always confusing Mind, however, brings flexibility. Namespaces can also use dynamic language features, but it should be noted that direct calls to namespaces are resolved at compile time, while dynamic features are not resolved at compile time. So be sure to add a prefix. For example:



Copy code

The code is as follows:

namespace MyObjectSub;
new Foo(); //Call MyObjectSubFoo( ), has been parsed into MyObjectSubFoo
$a = 'Foo';
new $a(); //Foo() is called, not MyObjectSubFoo()
$b = 'MyObjectSubFoo' ; //Equivalent to MyObjectSubFoonew $b(); //Call MyObjectSubFoo()//If you use double quotes, use them, such as $a = "MyObjectSub";
Appendix 1: The same file defines multiple namespaces
There are two methods:




Copy code

The code is as follows:

namespace MyProject;
const CONNECT_OK = 1;

class Connection { / * ... */ }

function connect() { /* ... */ }

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

Method 1, keep a running account.



Copy code

The code is as follows:

namespace MyProject {const CONNECT_OK = 1;

class Connection { / * ... */ }
function connect() { /* ... */ }

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



Method 2, use braces to put the code in the same namespace inside braces. This method requires that there cannot be any code other than declare outside the curly braces. For global scope code, use curly braces without space names.




http://www.bkjia.com/PHPjc/736840.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/736840.html

TechArticle

1. What is a namespace? A namespace is a special scope that contains identifiers within that scope and is itself an identifier. You can combine namespaces with operations...
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