包含来自PHP流的代码

包含来自PHP流的代码,php,include,stream,Php,Include,Stream,我想知道是否有可能创建一个流包装器,以便将一些代码从一个数组加载到下面的代码中 <?php include 'template://myarraykey/'; ?> 它的工作方式是否与从文件中执行普通include一样?问这个问题的原因是因为我不想将模板存储在文件系统中,它们要么存在于memcache中,要么存在于数据库表中,当然不想使用eval() 此外,我还假设需要将allow\u url\u include设置为on?include可以获取任意url。阅读下面是一个示例H

我想知道是否有可能创建一个流包装器,以便将一些代码从一个数组加载到下面的代码中

<?php include 'template://myarraykey/'; ?>

它的工作方式是否与从文件中执行普通include一样?问这个问题的原因是因为我不想将模板存储在文件系统中,它们要么存在于memcache中,要么存在于数据库表中,当然不想使用eval()


此外,我还假设需要将allow\u url\u include设置为on?

include可以获取任意url。阅读下面是一个示例HTTP代码:

<?php

/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.

?>

eval这个词不是邪恶的。你可以用它做的事情是。任何你想做的事情都会有与评估相同的风险。所以只需使用eval,因为保护它是一个更“已知”的问题。

因为
include
可以使用任何合适的流,并且您可以注册自己的流包装器,我不明白为什么不可以


只是为了好玩,您可以尝试另一种选择:从memcached加载数据,并使用。

我知道这是一个老问题……但我认为值得注意的是,您可以执行以下操作:

$content = '
  <?php if($var=true): ?>
    print this html
  <?php endif; ?>
';

它会很快地解析它

经过一些编码和测试后,我得出结论,使用流与邪恶()有相同的问题,因此我选错了树在本例中,我决定将其存储在DB中,并缓存到文件系统中,这样它就可以从APC缓存中获益,并且在每次编辑模板后,我都会更新文件系统缓存。这只有在启用了allow_url_include时才起作用。
include "data://text/plain;base64,".base64_encode($content);