PHP使用extract()将数组值作为变量传递并在页面上显示

时间:2017-08-03 03:53:53

标签: php function

我正在尝试在view($path,$data)中创建一个php函数,主要功能是包含目录中的特定文件,并将数据/变量传入该页面,我设法创建{{1}并且能够包含定义路径,现在我的下一步是将$path值传递到我的包含页面中,并希望将每个数组标签转换为变量。

我的php课程在$data以下。

classes.php

在我的index.php上

define("SITE_NAME", "process");

 class helpers {


        public function view($path, $data) 
        {

            $dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";

            include($dir.$path.".php");

            return extract($data);



        }  

}

所以现在在我的test.php上,我想回复require_once('classes.php'); $helper = new Helpers(); $data['title'] = "My Test"; $data['test'] = "test1"; $helper->view('test',$data); ,我假设它将返回$title的值,以测试我是否从My Test获取值能够使用index.php

输出$data

print_r

任何建议我如何实现?我正在尝试Array ( [title] => My Test [test] => test1 )函数,但不知道我的语法是否正确。提前谢谢!

1 个答案:

答案 0 :(得分:4)

因为返回仅限于一个值。

通过提取它,您将获取一个值the array并将其拆分为多个。

我个人避免在代码中使用extract$$var之类的内容,因为它会破坏我的IDE并使可读性几乎不可能。也就是说,在这种情况下使用它确实有意义,因为它在有限的范围内限制了无意中意外覆盖另一个变量的可能性。

http://php.net/manual/en/functions.returning-values.php

  

函数不能返回多个值,但返回数组可以获得类似的结果。

http://php.net/manual/en/function.extract.php

  

extract - 从数组

将变量导入当前符号表

符号表〜范围

另外

  

返回值:返回成功导入的变量数   进入符号表

public function view($path, $data)
{

    $dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";

    include($dir.$path.".php");
    return extract($data); //returns the variables successfully imported from $data
}

正如您在调用return时所看到的那样,结束当前函数的执行,并关闭该范围。您将不得不重新安排这些,因此首先进行变量赋值。

我假设方法中包含的文件是这样的,它不在OP中。

    <h1><?= $title; ?></h1>

您在技术上不需要返回任何内容,因为HTML自然会被输出缓冲区捕获并在脚本完成执行时显示。但是,这不是很干净。正确的方法是控制输出缓冲区,如下所示:

    public function view($path, $data) 
    {
        $dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";

        extract($data); //variables must exist before importing where they are used.
        ob_start();
        include($dir.$path.".php"); //capture this output
        $view = ob_get_clean();
       return $view;
    }  

   $out = $helper->view('test',$data); //out now contains the HTML output from the included file.

然后你可以echo出来。 IMO这样做更好,因为您可以将该HTML输出插入另一个数据数组并将其传输到另一个模板中。这对于可重复使用的页眉或页脚或导航栏等内容非常有用。

考虑一下

$head['page_title'] = "My Test";
$body['head'] = $helper->view('header',$head); //create head and assign to body

$body['name'] = 'John Smith';
echo $helper->view('body',$body); //create body (with head inserted) and echo

并且header.php

<title><?= $page_title; ?></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

和body.php

<!DOCTYPE html>
<html>
    <head>
        <?= $head; ?>
    </head> 
    <body>
       <p><?= $name; ?></p>
    </body> 
 </html>

输出现在是这样的

<!DOCTYPE html>
<html>
   <head>
      <title>My Test</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  </head>   
  <body>
     <p>John Smith</p>
  </body>   
</html>

现在两个页面合并为一个,即输出。你可以将它带到你想要的任何级别的可重用性,但它可以节省大量的打字并使视图更容易维护。

但是,正如我所说,你可以简单地允许它自然输出

    public function view($path, $data) 
    {
        $dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";

        extract($data); //variables must exist before importing where they are used.
        include($dir.$path.".php"); //capture this output
    } 
    //outputs when script is done.