有什么办法可以临时使用命名空间吗? 我正在使用一个库来创建表单,它使用命名空间,问题是我通常想在页面中间创建一个表单,因此位于全局命名空间中。然后,如果我想调用这个库的任何函数,我必须在所有内容前面加上命名空间\
PHP 中没有办法做这样的事情吗:
Blabla global namespace
strlen('test'); // 4
namespace Namespace
{
test();
}
More global PHP
让它引用命名空间\test?
最佳答案
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 ?>
这大约是您能得到的最接近的结果 - 无法暂时更改默认命名空间。
关于php - 临时命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9969829/