【问题标题】:PHP - variable inclusion not working with include_once inside a namespacePHP - 变量包含不适用于命名空间内的 include_once
【发布时间】:2016-07-07 23:13:01
【问题描述】:

我正在使用不同的模式(基本的用户名和密码组合以及另一个使用 Yubikey 的模式,目前)对登录页面进行建模。

我的控制器如下所示:

namespace Document {
    /**
     * get the current authentication schema
     */
    $schema = \Modules\Backend\Authentication::getSchema();

    /**
     * initialize the template data
     */
    if (empty($data)) {
        $data = [];
    }

    /**
     * include the document content block
     */
    $data = array_merge_recursive($data, [
        "document" => [
            "sections" => [
                /* further content goes here */
            ]
        ]
    ]);

    /**
     * include the authentication schema content block
     */
    if (file_exists($schema = "{$_SERVER["DOCUMENT_ROOT"]}/pages/controllers/backend/login/{$schema}.php")) {
        include_once($schema);
    }

    /**
     * output the document content
     */
    echo \Helpers\Templates::getTemplate("backend/pages/login", $data);

    /**
     * free all used resources
     */
    unset($data, $schema);
}

身份验证架构如下所示:

/**
 * include the document content block
 */
$data = array_merge_recursive(!empty($data) ? $data : [], [
    "document" => [
        "sections" => [
            "schema" => [
                "content" => [
                    "token" => \Helpers\Strings::getToken(),
                    /* rest of content block goes here */
                ],
                "strings" => [
                    "title" => _("This is a sample string"),
                    /* rest of translation strings block goes here */
                ]
            ]
        ]
    ]
]);

我遇到的问题是include_once() 对我不起作用,因为$data 变量并没有真正被身份验证架构看到,也没有被相反(文档命名空间看到任何来自身份验证架构的内容) )。

但是,如果我使用include(),它可以工作。也许问题在于名称空间的使用和包括外部内容。我从不使用 include() 函数,因为我总是喜欢检查脚本是否已包含在内,即使额外检查会导致性能下降。

也许我没有完全理解命名空间在 PHP 中是如何工作的,或者我对 array_merge_recursive() 函数做了一些奇怪的事情,但是我查看代码越多,发现潜在错误的地方就越少,我感到有点失落.

谁能帮我解决这个问题?

【问题讨论】:

  • 你确定这个include_once 只被调用了一次吗?如果您已经将它包含在其他地方怎么办?对于那种事情,当你想从某个文件中返回一堆数据时,我建议使用require
  • 我必须在阅读完所有 cmets 后仔细检查,但只有当任何给定用户尝试访问后端时才会加载此控制器(如果他/她的会话数据将被重定向到登录屏幕未找到),并且未在后端或前端的任何其他部分共享。

标签: php variables namespaces include-once


【解决方案1】:

您的脚本的一个简单设置显示,命名空间内的 include_once 实际上与 include 相同。看来,您之前在其他地方包含了 schema.php。

<?php

namespace Document {
    include_once "data.php";

    echo "Hello " . $data;
}

数据.php

<?php

$data = "World";

在您的情况下,您可以添加一些调试输出,以获取第一次包含您的方案的文件/行。即

var_dump(array_map(function($x) {
    return [$x['file'], $x['line']];
}, debug_backtrace()));

但我建议只使用 include 而不是 include_once 并返回数据,而不是创建新的变量。

即方案.php

return [
    "document" => [
        "sections" => [
            "schema" => [
                "content" => [
                    "token" => \Helpers\Strings::getToken(),
                    /* rest of content block goes here */
                ],
                "strings" => [
                    "title" => _("This is a sample string"),
                    /* rest of translation strings block goes here */
                ]
            ]
        ]
    ]
];

然后将其包含在

$data = include($schema . '.php");

并在需要的地方进行合并(和其他事情)。

【讨论】:

    猜你喜欢
    • 2014-01-24
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 2015-10-09
    相关资源
    最近更新 更多