【问题标题】:How can I get the variables of a file 'Index.php' that included 'Init.php', without reincluding 'Init.php'?如何在不重新包含“Init.php”的情况下获取包含“Init.php”的文件“Index.php”的变量?
【发布时间】:2015-06-01 17:38:28
【问题描述】:

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

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

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

<?php

require 'Init.php';

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

这就是我们两个文件的样子,但是,如果我说,在 $page['name'] 元素上使用变量而不是字符串,页面的标题实际上就是变量名(想象一下“站点名称 - $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' =&gt; '', 'name' =&gt; '', ),

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

index.php(更改值的地方):
array ( 'id' =&gt; 'index', 'name' =&gt; 'Test', )

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

【问题讨论】:

  • 您是否偶然在变量名周围使用了刻度?像这样的东西? $page['id'] = '$variable'; 如果是,请去掉勾号。
  • 关于上述注释的注释:PHP 不会计算用单引号括起来的字符串中的变量:'this is my $value' 不能像 "this is my $value"'this is my ' . $value 那样工作。
  • 为什么不使用 require_once?我只使用 require_once。
  • 好吧,变量问题只是一个例子,关键是,对于 Init.php,$page 数组是未定义的。然而,我已经删除了蜱虫,它没有任何区别。
  • @KevinNagurski 我不使用 require_once 因为我会将框架分发给其他开发人员,我不希望他们必须调试我的错误,很多人都更舒服使用 require 或 include。

标签: php variables include


【解决方案1】:

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

例子:

index.php

<?

require 'Init.php';

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

然后,我们在 Init.php 上完成所有复杂的框架性工作,但我们还包括内核,它将包含关闭函数

初始化.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

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

然后,您可以从Module 类将$page 数组传递给其他类/文件。

【讨论】:

  • $page = array('id', 'name'); 不会在那里做你想做的事。试试$page = array('id' =&gt; 0, 'name' = "");。或者简单地说:$page = array(); 并稍后定义索引。
【解决方案2】:

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

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', ) 

【讨论】:

  • 我很抱歉我的回答,看来我看得太快了,以为我修好了。我已经尝试过了,但元素的值仍然没有改变。
  • var_export($page); 添加到每个页面时,您会得到什么结果?
  • config.php 上返回array ( 'id' =&gt; '', 'name' =&gt; '', ),在Init.php 上返回array ( 'id' =&gt; '', 'name' =&gt; '', ),在index.php 上返回array ( 'id' =&gt; 'index', 'name' =&gt; 'Test', ),在pageTpl.php 上返回NULLTL;DR 新值仍然局限于 index.php
  • 好的,您的解释中缺少某些内容。 index.php 中是否包含 pageTpl.php
  • 不,pageTpl.php 包含在 Init.php 中
猜你喜欢
  • 2012-09-19
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
相关资源
最近更新 更多