如何从file_get_contents运行php代码

时间:2014-02-13 09:48:20

标签: php

我有一个文件 template.php

<html>
    <head>
        <title>{title}</title>
    </head>
    <body>
    <div id="header">
            <h1>This is header</h1> 
        </div>
        <div id="content">
            <h1>This is content</h1>
            <?php $this->showContent(); ?>
        </div>
    <div id="footer">
        <p>Copyright &copy; <?= date('Y'); ?> by My Company.</p>
    </div>
    </body>
</html>

并在文件 controll.php 中,代码如下所示:

$file = file_get_contents('template.php');
echo $file;

但它不运行php脚本$this->showContent();

有人能帮助我吗?

1 个答案:

答案 0 :(得分:4)

你应该试试

require_once('template.php'); // will execute the page as php

file_get_contents 将执行的操作是将文件作为某种文本文件读取并返回其值。那不是你想要的。 require_once.php 将包含php文件并将其作为php处理。这与将您包含在其中的文件中的代码类似。