This article introduces you to the content of the PHP namespace this part of the content of the detailed, there is a certain reference value, the need for a friend can refer to, I hope you have some help.
PHP's namespace (namespace) is only available after php5.3. This concept is already very early in C #, and the namespace in PHP is actually the same as the C # concept.
Why should I use namespace in PHP?
Assuming that you do not use namespace, the name of each class in a project must be fixed. Because PHP has a file that corresponds to a class name, whether it's calling AutoLoad or calling a loaded class at new. So when there is no namespace, we think of various naming conventions to distinguish different classes, such as Project1_school1_class1_student or project2_school_class_student.
The introduction of namespace can be effectively circumvented, a namespace equivalent to a file path, when looking for this class, it will go to the corresponding file path to find the class definition file.
Definition and use of namespace
Defined:
<?php
namespace Myproject;
Or
<?php
namespace Myproject {
}
Use:
<?php
use Myproject/School;
<?php
use Myproject/School as School1; // 别名
Namespaces are parsed at run time. Use is equivalent to a declaration that does not parse and load. For example, the following:
test.php
<?php
use my\name;
require_once("/home/yejianfeng/handcode/test/namespace1.php");
$a = new my\name\A();
$a->Print1();
namespace1.php
<?php
namespace my\name;
class A {
public function Print1(){
echo 11;
}
}
Although the require_once is under use, it can be run normally, because the program only loads the namespace when new My\name\a () is loaded my\name
Global classes and Namespace classes
If you want to new a global class using new \a ()
If you want to new a namespace class, use new My\namespace\a ()
The Order of namespaces
The most error-prone thing to do since you have a namespace is what the search path for this class is when you use the class.
If we can figure out this example in manual, we can all find out the order.
<?php
namespace A;
Use b\d, c\e as F;
Function call
Foo (); First try to invoke the function foo () defined in namespace "A"
Try calling the global function "foo" again
\foo (); Call Global space function "Foo"
My\foo (); Call definition in Namespace "a\my" in function "foo"
F (); First attempt to invoke the function "F" defined in namespace "a"
Try calling the global function "F" again
Class reference
New B (); Create an object of class ' B ' defined in namespace ' a '
If not found, attempt to automatically load class "a\b"
New D (); Use an import rule to create an object of class ' D ' defined in namespace ' B '
If not found, attempt to automatically load class "B\d"
New F (); Use an import rule to create an object of class ' E ' defined in the namespace ' C '
If not found, attempt to automatically load class "C\e"
New \b (); Create an object that defines the class "B" in the global space
If it is not found, try to automatically load class "B"
New \d (); Create an object that defines the class "D" in the global space
If it is not found, try to automatically load class "D"
New \f (); Create an object that defines the class "F" in the global space
If not found, attempt to automatically load class "F"
Call a static method or namespace function in another namespace
B\foo (); Call namespace "a\b" in function "foo"
B::foo (); Call the "Foo" Method of Class "B" defined in namespace "A"
If the class "a\b" is not found, try to automatically load the class "a\b"
D::foo (); Use the import rule to invoke the "Foo" Method of class ' D ' defined in namespace ' B '
If the class "b\d" is not found, try to automatically load the class "B\d"
\b\foo (); Call the function "foo" in the namespace "B"
\b::foo (); Call the "Foo" Method of Class "B" in global space
If class "B" is not found, try to automatically load class "B"
static methods or functions in the current namespace
A\b::foo (); Call the "Foo" Method of Class "B" defined in the namespace "A\a"
If the class "a\a\b" is not found, try to automatically load the class "a\a\b"
\a\b::foo (); Call the "Foo" Method of Class "B" defined in the namespace "a\b"
If the class "a\b" is not found, try to automatically load the class "a\b"
?>