<?php
namespace MyProject;
class MyClass {
static function static_method()
{
echo 'Hello, world!';
}
}
// Unqualified name, resolves to the namespace you are currently in (MyProject\MyClass)
MyClass:static_method()
(2)限制的名称
这是我们访问子命名空间层级的方式,这种方式要使用反斜杠语法。
<?php
namespace MyProject;
require 'myproject/database/connection.php';
// Qualified name, instantiating a class from a sub-namespace of MyProject
$connection = new Database\Connection();
下面的例子抛出了一个严重的错误:“Fatal error: Class 'MyProject\Database\MyProject\FileAccess\Input' not found" .”
这是因为 Myproject\FileAccess\Input 访问的是相对于你当前所在的命名空间的。
<?php
namespace MyProject\Database;
require 'myproject/fileaccess/input.php';
// Trying to access the MyProject\FileAccess\Input class
$input = new MyProject\FileAccess\Input()
<?php
namespace MyProject\Database;
require 'myproject/fileaccess/input.php';
// Trying to access the MyProject\FileAccess\Input class
// This time it will work because we use the fully qualified name, note the leading backslash
$input = new \MyProject\FileAccess\Input();
<?php
namespace MyProject;
var_dump($query); // Overloaded
\var_dump($query); // Internal
// We want to access the global Exception class
// The following will not work because there's no class called Exception in the MyProject\Database namespace and unqualified class names do not have a fallback to global space
// throw new Exception('Query failed!');
// Instead, we use a single backslash to indicate we want to resolve from global space
throw new \Exception('ailed!');
function var_dump() {
echo 'Overloaded global var_dump()!<br />'
} 动态调用
PHP 是一种动态的编程语言;因此也可以应用这种功能于命名空间的调用上。这本质上类似于实例化变量类 或者 包含变量文件。PHP命名空间的分隔符也是一个元字符。别忘了当你把命名空间存储于一个自动串变量中的时候去掉前面的反斜杠。
<?php
namespace OtherProject;
$project_name = 'MyProject';
$package_name = 'Database';
$class_name = 'Connection';
// Include a variable file
require strtolower($project_name . '/'. $package_name . '/' . $class_name) . '.php';
// Name of a variable class in a variable namespace. Note how the backslash is escaped to use it properly
$fully_qualified_name = $project_name . '\\' . $package_name . '\\' . $class_name;
$connection = new $fully_qualified_name();
<?php
namespace MyProject;
function run()
{
echo 'Running from a namespace!';
}
// Resolves to MyProject\run
run();
// Explicitly resolves to MyProject\run
namespace\run();
六、别名或者导入 “没有必要一定要使用命名空间”
PHP中的命名空间支持导入 importing .导入也叫做 别名。只用类、接口、命名空间可以取别名或者被导入。
导入是非常有用的,也是命名空间非常重要的一方面。它使你有能力去使用外部的包代码,像类库,不需要担心命名冲突。导入是通过 使用 use 关键字来支持的。制定一个自定义的别名 跟着 as 关键字。
use [name of class, interface or namespace] as [optional_custom_alias]
导入时怎样实现的呢
How it's Done
一个完全限制的名称 可以取一个短一点的非限制的名称,这样你当你想用它的时候不用每次都写一个长长的完全限制的名称。别名或者导入应该发生(或者叫使用)于最高层级的作用域或者是全局空间中。 尝试在一个方法或是函数的作用域中使用导入时不合法(不和语法的)的。
<?php
namespace OtherProject;
// This holds the MyProject\Database namespace with a Connection class in it
require 'myproject/database/connection.php';
// If we want to access the database connection of MyProject, we need to use its fully qualified name as we're in a different name space
$connection = new \MyProject\Database\Connection();
// Import the Connection class (it works exactly the same with interfaces)
use MyProject\Database\Connection;
// Now this works too! Before the Connection class was aliased PHP would not have found an OtherProject\Connection class
$connection = new Connection();
// Import the MyProject\Database namespace
use MyProject\Database;
$connection = new Database\Connection()
或者,你可以取一个不同的名字作为别名。
<?php
namespace OtherProject;
require 'myproject/database/connection.php';
use MyProject\Database\Connection as MyConnection;
$connection = new MyConnection();
use MyProject\Database as MyDatabase;
$connection = new MyDatabase\Connection();
<?php
namespace MyProject;
// Fatal error: Class 'SomeProject\Exception' not found
throw new Exception('An exception!');
// OK!
throw new \Exception('An exception!');
// Import global Exception. 'Exception' is resolved from an absolute standpoint, the leading backslash is unnecessary
use Exception;
// OK!
throw new Exception('An exception!');
尽管可以动态调用命名空间的代码, 但是动态的导入是不支持的。
<?php
namespace OtherProject;
$parser = 'markdown';
// This is valid PHP
require 'myproject/blog/parser/' . $parser . '.php';
// This is not
use MyProject\Blog\Parser\$parser;