我有一个看起来像这样的文件:
<?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中的命名空间语句?
或者根本不可能?
答案 0 :(得分:2)
调用名称空间中的函数时必须指定名称空间,如下所示:
// test.php
namespace test;
function doesItWork() {
return 'It Works!';
}
// index.php
include 'test.php';
echo test\doesItWork();
如果要使用在当前命名空间之外的命名空间中定义的函数,则必须在其前面添加命名空间字符,如下所示:
echo \test\doesItWork();