2012-04-02 62 views
2

有没有什么办法可以临时使用命名空间? 我正在使用一个库来创建窗体,它使用命名空间,问题是我通常希望在页面中间创建窗体,因此在全局命名空间中。然后,如果我想调用这个库我必须前缀命名空间的一切\临时命名空间

的任何功能,这难道没有PHP没有办法做这样的事情:

Blabla global namespace 
strlen('test'); // 4 

namespace Namespace 
{ 
    test(); 
} 

More global PHP 

而且有它指的命名空间\测试?

回答

3

http://www.php.net/manual/en/language.namespaces.importing.php

<?php 
namespace foo; 
use My\Full\Classname as Another; 

// this is the same as use My\Full\NSname as NSname 
use My\Full\NSname; 

// importing a global class 
use ArrayObject; 

$obj = new namespace\Another; // instantiates object of class foo\Another 
$obj = new Another; // instantiates object of class My\Full\Classname 
NSname\subns\func(); // calls function My\Full\NSname\subns\func 
$a = new ArrayObject(array(1)); // instantiates object of class ArrayObject 
// without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject 
?> 

这是你能得到的最接近的 - 有没有办法临时更改默认的命名空间。

+0

好吧,这可能是够好的。快速提问:我在全局命名空间中使用了一组通过自动加载器导入的类。现在,如果我在开始时声明它们都是有用的,但是它们在被调用时仍然被加载,它会改变任何性能方面的东西吗? – 2012-04-02 01:34:13

+0

它们将通过使用语句加载,而不是在它们被调用时加载。它不应该改变任何性能明智的,只是你的自动加载器包含的另一个文件。 – 2012-04-02 01:54:13