我有一个文件,我需要在其中指定多个命名空间中的类,例如:
<?php
namespace library;
class ClassInLib {
...
}
namespace \; //Switch to root namespace
class ClassInRoot {
...
}
上面的代码在namespace\;
处有一个语法错误。如何从 library
命名空间切换到根命名空间?
我为什么需要这个:我需要在单元测试期间模拟一堆类,我认为这些非常短的模拟类不适合放在单独的文件中。
最佳答案
namespace
{
class RootClass
{
function whatever();
}
}
namespace Symfony\Component\DependencyInjection
{
interface ContainerAwareInterface
{
function setContainer(ContainerInterface $container = null);
}
}
http://www.php.net/manual/en/language.namespaces.definitionmultiple.php
无论如何,您很有可能会决定使用单独的文件。
关于PHP 根命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9812295/