我有一个名为test.php的命名空间文件,它带有一个函数和一个类:
namespace Test;
function testFunc(){}
class TestClass{}
Run Code Online (Sandbox Code Playgroud)
然后,如果在另一个文件中我"使用"这两个命名空间元素,则该类可以工作,但不能使用该函数:
use Test\testFunc,
Test\TestClass;
include "test.php";
new TestClass();
testFunc();
Run Code Online (Sandbox Code Playgroud)
TestClass对象创建正常,但我得到testFunc()的致命错误:
Fatal error: Call to undefined function testFunc()
Run Code Online (Sandbox Code Playgroud)
我认为命名空间支持函数.我究竟做错了什么?
编辑:这里的解释 - http://www.php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.nofuncconstantuse
请参阅http://php.net/manual/en/language.namespaces.rules.php,特别注意:
<?php
namespace A;
use B\D, C\E as F;
// function calls
foo(); // first tries to call "foo" defined in namespace "A"
// then calls global function "foo"
\foo(); // calls function "foo" defined in global scope
my\foo(); // calls function "foo" defined in namespace "A\my"
F(); // first tries to call "F" defined in namespace "A"
// then calls global function "F"
Run Code Online (Sandbox Code Playgroud)
和
// static methods/namespace functions from another namespace
B\foo(); // calls function "foo" from namespace "A\B"
B::foo(); // calls method "foo" of class "B" defined in namespace "A"
// if class "A\B" not found, it tries to autoload class "A\B"
D::foo(); // using import rules, calls method "foo" of class "D" defined in namespace "B"
// if class "B\D" not found, it tries to autoload class "B\D"
\B\foo(); // calls function "foo" from namespace "B"
\B::foo(); // calls method "foo" of class "B" from global scope
// if class "B" not found, it tries to autoload class "B"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6218 次 |
| 最近记录: |