名称空间语句可以是“”吗;包括“;在PHP中


Can the namespace statement be "included" in PHP?

我有一个文件,如下所示:

<?php
namespace n;
f(); // this calls n'f()

使用include函数可以包含PHP语句,但这对namespace语句不起作用:

<?php
include('example_include_namespace_statement.php')
f(); // this doesn't call n'f()

example_include_namespace_statement.php:

<?php
namespace n;

有没有办法在PHP中"包含"名称空间语句?

还是根本不可能?

当调用命名空间中的函数时,必须指定命名空间,如下所示:

// test.php
namespace test;
function doesItWork() {
    return 'It Works!';
}
// index.php
include 'test.php';
echo test'doesItWork();

如果要使用在当前命名空间之外的命名空间中定义的函数,则必须在其前面加上命名空间字符,如下所示:

echo 'test'doesItWork();

您应该阅读有关名称空间的文档。