无法在 PHP 中导入/使用命名空间函数

发布于 2024-11-01 04:10:33 字数 717 浏览 1 评论 0原文

我有一个名为 test.php 的命名空间文件,其中包含一个函数和一个类:

namespace Test;
function testFunc(){}
class TestClass{}

然后,如果在另一个文件中我“使用”这两个命名空间元素,则该类可以工作,但该函数不起作用:

use Test\testFunc,
    Test\TestClass;

include "test.php";
new TestClass();
testFunc();

TestClass 对象创建得很好,但是我收到 testFunc() 的致命错误:

Fatal error: Call to undefined function testFunc()

我认为命名空间支持函数。我做错了什么?

编辑:此处的说明 - http://www.php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.nofuncconstantuse

I've got a namespaced file called test.php with a function and a class:

namespace Test;
function testFunc(){}
class TestClass{}

Then if, in another file I "use" both of these namespace elements, the class works but not the function:

use Test\testFunc,
    Test\TestClass;

include "test.php";
new TestClass();
testFunc();

The TestClass object is created fine, but I get a fatal error for testFunc():

Fatal error: Call to undefined function testFunc()

I thought functions were supported with namespaces. What am I doing wrong?

EDIT: Explanation here - http://www.php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.nofuncconstantuse

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

涫野音 2024-11-08 04:10:33

请参阅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"

并且

// 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"

See http://php.net/manual/en/language.namespaces.rules.php with particular attention to:

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

And

// 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"
南薇 2024-11-08 04:10:33

从 PHP 5.6 及更高版本,您可以导入/使用其他 PHP 文件中的函数,如下所示:

require_once __DIR__ . "/../../path/to/your/vendor/autoload.php";
use function myprogram\src\Tools\MyFunc;
//use the imported function
MyFunc();

但是,对于 PHP 7.0,我需要将该函数添加到composer.json 中的“文件”中:

"autoload" : {
    "psr-4" : {
      "myprogram\\src\\" : "myprogram/src/"
    },
"files" : [
      "myprogram/src/Tools/ScriptWithMyFunc.php"
    ]

然后运行 ​​composer dump-autoload 更新 autoload.php。

或者:

您也可以直接从脚本导入函数,而无需编写器:

require_once full\path\to\ScriptWithMyFunc.php;
MyFunc();

但是(至少对我来说)这只在 ScriptWithMyFunc.php 没有命名空间时才有效。

From PHP 5.6 and higher you can import/use functions from other PHP files as follows:

require_once __DIR__ . "/../../path/to/your/vendor/autoload.php";
use function myprogram\src\Tools\MyFunc;
//use the imported function
MyFunc();

However, for PHP 7.0, I needed to add the function to "files" in the composer.json:

"autoload" : {
    "psr-4" : {
      "myprogram\\src\\" : "myprogram/src/"
    },
"files" : [
      "myprogram/src/Tools/ScriptWithMyFunc.php"
    ]

And then run composer dump-autoload to update autoload.php.

ALTERNATIVELY:

You can also import functions from scripts directly without composer:

require_once full\path\to\ScriptWithMyFunc.php;
MyFunc();

But (at least for me) this only works when ScriptWithMyFunc.php does not have a namespace.

叹梦 2024-11-08 04:10:33

我相信命名空间函数(函数和常量的 use 关键字的端口)将成为 PHP 5.6 的一部分

另请参阅:

https://github.com/php/php-src/pull/526

https://wiki.php.net/rfc/use_function

I believe namespaced functions (port of use keyword for function and constants) will be part of PHP 5.6

See also:

https://github.com/php/php-src/pull/526

https://wiki.php.net/rfc/use_function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文