【问题标题】:Get variables from included file从包含的文件中获取变量
【发布时间】:2014-08-01 01:46:05
【问题描述】:

如何使用包含文件中的变量,并将其用于其他包含文件?

索引

<?php
$tmp = new template($connect);
$tmp->globals('index');
$logged_in = false; //works in all included files
?>
<html>
  <head>
    <?php $tmp->template('head'); ?> //class method to include file
  </head>
  <body>
    <?php echo $description; ?> //does not work either

include_head.php

 <title><?php echo $title; ?></title>//does not echo anything

index_globals.php

<?php
    $title="title";
    $description="description";   
 ?>

我如何参与

public function template($file){
    if(isset($file) && file_exists($this->dir.$file.".php")){
        ob_start();
        include($this->dir.$file.".php");
        $template = ob_get_contents();
        return $template;
     }
}

全局函数

public function globals($name){
  if(isset($name) && file_exists($this->dir.$name."_globals.php")){
      include($this->dir.$name."_globals.php");
  }
}

【问题讨论】:

  • 你是如何包含文件的?
  • 你试过echo $GLOBALS['title']吗? $description 也是如此。 documentation中有一个特殊的部分
  • 查看更新。我现在就试试$GLOBALS 方法——那个方法也不管用。

标签: php variables include require-once


【解决方案1】:

当您使用include()require() 在PHP 中包含一个文件或将该文件带入另一个文件时,包含的文件基本上嵌入到包含它的文件中。这就像将包含文件中的所有代码写入调用include(*file*)的文件中。

简单地说:当您包含一个文件时,如果您使用include()require() 或类似上述方法成功包含它,则它的所有变量都可以像声明的任何其他变量一样使用。

【讨论】:

  • 是的,我读过,但它不适合我自己。我还测试了该文件是否实际上正在被解析并且它是
【解决方案2】:

您可以通过返回数组而不是声明变量来“导入”全局变量:

<?php
// index_globals.php

return [
    'title' => 'title',
    'description' => 'description',
];

然后,从globals() 函数将其导入本地属性:

private $context = [];

public function globals($name)
{
    if (isset($name) && file_exists($this->dir.$name."_globals.php")) {
        $this->context = include($this->dir.$name."_globals.php");
    }
}

最后,更新template()方法:

public function template($file)
{
    if (isset($file) && file_exists($this->dir.$file.".php")) {
        extract($this->context);
        ob_start();
        include($this->dir.$file.".php");
        $template = ob_get_contents();
        return $template;
     }
}

请注意,在这种情况下,您的索引也无法访问 $description,但通过 template 实例获得访问权限应该不难。

【讨论】:

  • 我编辑了您使用的代码 [],而它应该是 array()。除此之外,它就像一个魅力!
  • @EasyBB [] 语法从 5.4 开始就已经存在,是时候升级了 :)
  • PHP 版本 5.2.* 该死的 000webhost。好在我只将它们用于测试目的,而不是托管我的实际网站哈哈。非常感谢杰克。你帮助了我,而且你在这里教了我一个很好的小课!无法解释我的感激之情!
【解决方案3】:

您需要将变量注入到存储的属性中。

$tmp->set(array(
    'title' => 'hello world',
    'description' => 'this is the value'
));

// Or set a single value
$tmp->set('myCoolVariable', 'this is another value');

实施:

class template {
     protected $vars = array();

     public function set($key, $value)
     {
         if (is_array($key)) {
             // merge into existing
             $this->vars = array_merge($this->vars, $key);
         } else {
             // set a new variable with the name $key and value as $value
             $this->vars[$key] = $value;
         }
     }
}

然后在您的输出缓冲区方法中,extract() 存储的变量

public function template($file)
{
    if (isset($file) && file_exists($this->dir.$file.".php")) {
        ob_start();
        extract($this->vars); // extract it so it is available for the current buffer
        include($this->dir.$file.".php");
        $template = ob_get_contents();
        ob_end_clean(); // don't forget to clean and turn it off
        return $template;
     }
}

【讨论】:

  • 那么这可以在两个不同的文件之间工作吗?说 index.php 和 *_globals.php?
猜你喜欢
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 2012-09-15
  • 1970-01-01
相关资源
最近更新 更多