【问题标题】:namespacing functions in phpphp中的命名空间函数
【发布时间】:2017-05-10 01:00:58
【问题描述】:

我在命名空间和函数方面遇到问题。我正在使用 php 7。

我有一个文件:

namespace profordable\app;

function get_app() {
    if (!class_exists('profordable\app\app')) {
        return false;
    }
    global $global;
    if (!isset($$global)) {
        return false;
    }
    return $$global;
}

class app {
    ...
}

还有我的主索引文件:

namespace profordable\app;
$global = 'app';
$app = new app;

另一个文件为 FILE(全局命名空间):

function some_function() {
    $app = get_app();
}

我似乎无法在 FILE 中获取 get_app 函数,我已经对其进行了测试并且...

var_dump(function_exists('profordable\app\get_app');

返回 true,但是当我尝试(在 FILE 中)的组合时...

use profordable\app;
use function profordable\app\get_app;
function some_function() {
    $app = get_app();
    $app = profordable\app\get_app();
    $app = \get_app();
    ...etc
}

它不起作用。请帮忙

【问题讨论】:

  • 它做什么或不做什么?错误是什么?
  • 没有错误,get_app() 只是返回null

标签: php function namespaces


【解决方案1】:

这在 FILE 中起到了作用:

use function profordable\app\get_app as get_app;

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 1970-01-01
    • 2020-04-23
    • 2012-11-07
    • 1970-01-01
    • 2015-03-15
    • 2018-09-14
    • 2016-03-29
    • 1970-01-01
    相关资源
    最近更新 更多