doujiao7483 2014-05-25 01:44 采纳率: 0%
浏览 47
已采纳

包装后的未定义变量包含在PHP中

I'm using PHP for web development. I'm using the following function to wrap the include of a view:

<?php
function render($templateFile) {
    $templateDir = 'views/';
    if (file_exists($templateDir . $templateFile)) {
        include $templateDir . $templateFile;
    } else {
        throw new Exception("Template '{$templateFile}' couldn't be found " .
           "in '{$templateDir}'");
    }
}
?>

Although this seems right to me, there is a really unexpected behavior: when I define a variable to something (e.g. an array) and use render for including a view that uses that variable, I get an undefined variable error. But when I explicitely use include there is no error at all and things are just fine.

This is the script that calls render:

<?php
include 'lib/render.php'; // Includes the function above.

$names = array('Trevor', 'Michael', 'Franklin');

render('names.html'); // Error, but "include 'views/names.html'" works fine.
?>

And this is the file that uses the $names variable:

<html>
  <head>
    <title>Names</title>
  </head>
  <body>
    <ol>
    <?php foreach ($names as $name): ?>
      <li><?php echo $name; ?></li>
    <?php endforeach; ?>
    </ol>
  </body>
</html>

Help will be very much appreciated.

  • 写回答

2条回答 默认 最新

  • duanjianao0592 2014-05-25 01:54
    关注

    This is from the PHP documentation on the include function (c.f. http://us1.php.net/manual/en/function.include.php):

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

    And also:

    If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.

    So, if your render function can't access $names, then neither can your included file.

    A possible solution would be to pass the parameters you want to be able to access in your view template, to your render function. So, something like this:

    function render($templateFile, $params=array()) {
        $templateDir = 'views/';
        if (file_exists($templateDir . $templateFile)) {
            include $templateDir . $templateFile;
        } else {
            throw new Exception("Template '{$templateFile}' couldn't be found " .
               "in '{$templateDir}'");
        }
    }
    

    Then, pass them like this:

    $names = array('Trevor', 'Michael', 'Franklin');
    
    render('names.html', array("names" => $names));
    

    And use them in your view template like this:

    <html>
      <head>
        <title>Names</title>
      </head>
      <body>
        <ol>
        <?php foreach ($params['names'] as $name): ?>
          <li><?php echo $name; ?></li>
        <?php endforeach; ?>
        </ol>
      </body>
    </html>
    

    There are probably better solutions to this, like putting your render function into a View class. Then you can call the View class function from inside your template file, and access parameters that way instead of just assuming there will be a $params variable in the view templates scope. But, this is the simplest solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作