如何在不重新包含 'Init.php' 的情况下获取包含 'Init.php' 的文件 'Index.php' 的变量?

How can I get the variables of a file 'Index.php' that included 'Init.php', without reincluding 'Init.php'?

我目前正在对使用 MVC 的小型个人框架进行一些升级。

目前的工作方式是,当Init.php包含在某个文件中时,它不是寻找变量,而是获取文件的文本内容(实际源代码)并且只是"cuts out" 变量。我认为它非常不正统,老实说,很糟糕。

一位开发人员也开发了一个同样使用 MVC 的框架,并且能够以正确的方式完成我想做的事情。

<?php

require 'Init.php';

$page['id'] = 'index';
$page['name'] = 'Home';

这就是我们两个文件的样子,但是,如果我要说的话,在 $page['name'] 元素上使用变量而不是字符串,页面的标题会字面意思是变量名(假设 "Sitename - $variable")

我找了大约 2 天的答案,我找到了一个有前途的答案,基本上使用 require_once 和 ob_get_contents,但是,我不想使用 require_once.

我怎样才能做我的开发伙伴所做的事情?

编辑
这是我目前尝试获取数组的尝试,它仅在使用 require_once.

时有效
/************* CONTENT PARSING **************/

global $page;

$buffer = explode('/', $_SERVER['PHP_SELF']);
$filename = $buffer[count($buffer) - 1]; // index.php in our case

var_dump($page); // Dumps NULL

ob_start();

include($filename);

echo $page['id']; // Echoes nothing

echo ob_get_contents(); // Echoes nothing

echo $page['id']; // Dumps nothing

ob_flush(); // Returns nothing

var_dump($page); // Dumps nothing

编辑 2
这是包含文件和声明变量的方式

config.phppageTpl.php 包含在 Init.php

config.php 包含 $page 数组并包含在 pageTpl.php

之前

index.php 包括 Init.php

简而言之,我想分配给 $page 数组的 idname 元素的值只有在 index.php,我希望开发人员可以全局访问变量。 (其中包含 Init.php)

我尝试对每个文件 运行 var_extract($page),结果如下:

config.php(声明$page数组的地方):
array ( 'id' => '', 'name' => '', ),

Init.php(其中包含 config.php):
array ( 'id' => '', 'name' => '', ),

index.php(值发生变化的地方):
array ( 'id' => 'index', 'name' => 'Test', )

pageTpl.php(文件包含在 Init.php 中,尝试访问 $page 数组):
NULL

好的,所以在浏览了几次文档并阅读了一些框架的代码之后,我注意到获得所有这些变量的最终值的唯一方法是使用 PHP register_shutdown_function 这是脚本执行完成后调用的函数,这意味着所有变量都经过处理、计算,因此只要它们是全局的,就可以从该函数访问。

示例:

index.php

<?

require 'Init.php';

$page['id'] = 'index';
$page['name'] = 'Home';

然后,在 Init.php 我们完成所有复杂的框架,但我们还包括内核,它将包含关闭功能

Init.php

<?

require 'inc/kernel.php';

new Kernel;

现在,当然是内核

kernel.php

<?

class Kernel
{
    public function __construct()
    {
        register_shutdown_function('Kernel::Shutdown'); // Registers shutdown callback
        spl_autoload_register('Kernel::LoadMod'); // Not exactly sure why is this function necessary, but it is. All I know is that it tries to files of called classes that weren't included
    }

    static function Shutdown()
    {
        global $page;

        var_dump($page); // Will print what we defined in the $page array

        Module::LoadTpl($page); // We pass the $page array to the tpl module that will do the rest
    }
}

module.php

class 模块 { 静态函数 LoadTpl($page) { var_dump($页); // 也会打印 $page,但现在我们不局限于 kernel.php } }

然后,您可以从 Module class 将 $page 数组传递给其他 classes/files。

当你定义一个带有索引的数组时,你需要以特定的方式来做。

config.php:

<?php 
$page = array('id' => "", 'name' => "");
var_export($page);
?>

这将创建名为 $page 的数组,索引为 idname,其值为空。

现在在您的 index.php 中,您可以分配值:

<html>
<body>
<pre>
<?php

include 'Init.php';

$page['id'] = basename($_SERVER['PHP_SELF']);
$page['name'] = 'Home';

var_export($page);
?>
</pre>
</body>
</html>

这应该导致页面显示:

array ( 'id' => '', 'name' => '', )
array ( 'id' => 'index.php', 'name' => 'Home', )