名称空间语句是否可以包括在内#34;用PHP?

时间:2014-10-30 00:13:04

标签: php namespaces php-include

我有一个看起来像这样的文件:

<?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;

有没有办法去&#34;包括&#34; PHP中的命名空间语句?

或者根本不可能?

1 个答案:

答案 0 :(得分:2)

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

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

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

echo \test\doesItWork();

您应该阅读documentation on namespaces