用php生成php脚本

看到这篇文章,很有趣:超链接地址Generating PHP with Ruby

下面是个用php生成php简单的例子:

< ?php
// Some code
$code = ‘
<?php
echo “Hello World!”;
?>
‘;

// Write code to file
file_put_contents(‘generated.php’,$code); # PHP5 function!

// Include the generated code for execution
include ‘generated.php’;
?>

为什么不直接使用eval呢?我的理解是,这样只需要将生成的代码包含进来直接运行便是,省去了eval的解析时间,效率可能会更高.

2 Replies to “用php生成php脚本”

Leave a Reply

Your email address will not be published.