PHP Scoped输出缓冲区,如何?

时间:2017-05-29 02:35:07

标签: php output-buffering

  

如何堆叠输出缓冲区以使每个缓冲区都有自己的ID?

因此,我有一个包含缓冲区的HTML页面,但也有一个与a hook相关联的页面内容。钩子本身也包含输出缓冲区:

<?php ob_start(); ?>
<html>
  <head></head>
  <body>
    <?php ob_start(); ?>
    <?php echo $page->content; ?>
    <?php echo ob_get_clean(); ?>
  </body>
</html>
<?php ob_get_clean(); ?>

第一个ob_get_clean()刚从HTML页面停止了输出缓冲区。

那么,有可能有这样的机制吗?

<?php ob_start('#template'); ?>
<html>
  <head></head>
  <body>
    <?php ob_start('#page'); ?>
    <?php echo $page->content; ?>
    <?php echo ob_get_clean('#page'); ?>
  </body>
</html>
<?php ob_get_clean('#template'); ?>

这样每个ob_get_clean()都有自己的值:

  • #template<html> … </html>
  • #page<p>Lorem ipsum dolor sit amet.</p>

我收到了这些错误:

[28-May-2017 09:17:01 Asia/Jakarta] PHP Fatal error:  Cannot destroy active lambda function in G:\server\www\lot\extend\plugin\lot\worker\block-e\index.php on line 5

[28-May-2017 09:17:01 Asia/Jakarta] PHP Stack trace:

[28-May-2017 09:17:01 Asia/Jakarta] PHP   1. {main}() G:\server\www\index.php:0

[28-May-2017 09:17:01 Asia/Jakarta] PHP   2. require() G:\server\www\index.php:48

[28-May-2017 09:17:01 Asia/Jakarta] PHP   3. Hook::fire() G:\server\www\engine\fire.php:117

[28-May-2017 09:17:01 Asia/Jakarta] PHP   4. call_user_func_array:{G:\server\www\engine\kernel\hook.php:79}() G:\server\www\engine\kernel\hook.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP   5. do_fire() G:\server\www\engine\kernel\hook.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP   6. Route::fire() G:\server\www\engine\fire.php:113

[28-May-2017 09:17:01 Asia/Jakarta] PHP   7. call_user_func_array:{G:\server\www\engine\kernel\route.php:140}() G:\server\www\engine\kernel\route.php:140

[28-May-2017 09:17:01 Asia/Jakarta] PHP   8. {closure:G:\server\www\lot\extend\page\index.php:46-168}() G:\server\www\engine\kernel\route.php:140

[28-May-2017 09:17:01 Asia/Jakarta] PHP   9. Shield::attach() G:\server\www\lot\extend\page\index.php:166

[28-May-2017 09:17:01 Asia/Jakarta] PHP  10. ob_end_flush() G:\server\www\engine\kernel\shield.php:90

[28-May-2017 09:17:01 Asia/Jakarta] PHP  11. Shield::{closure:G:\server\www\engine\kernel\shield.php:84-88}() G:\server\www\engine\kernel\shield.php:90

[28-May-2017 09:17:01 Asia/Jakarta] PHP  12. Hook::NS() G:\server\www\engine\kernel\shield.php:86

[28-May-2017 09:17:01 Asia/Jakarta] PHP  13. call_user_func_array:{G:\server\www\engine\kernel\hook.php:94}() G:\server\www\engine\kernel\hook.php:94

[28-May-2017 09:17:01 Asia/Jakarta] PHP  14. Hook::fire() G:\server\www\engine\kernel\hook.php:94

[28-May-2017 09:17:01 Asia/Jakarta] PHP  15. call_user_func_array:{G:\server\www\engine\kernel\hook.php:79}() G:\server\www\engine\kernel\hook.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP  16. fn_twitter_cards_replace() G:\server\www\engine\kernel\hook.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP  17. Page->__get() G:\server\www\engine\kernel\hook.php:8

[28-May-2017 09:17:01 Asia/Jakarta] PHP  18. Hook::NS() G:\server\www\engine\kernel\page.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP  19. call_user_func_array:{G:\server\www\engine\kernel\hook.php:94}() G:\server\www\engine\kernel\hook.php:94

[28-May-2017 09:17:01 Asia/Jakarta] PHP  20. Hook::fire() G:\server\www\engine\kernel\hook.php:94

[28-May-2017 09:17:01 Asia/Jakarta] PHP  21. call_user_func_array:{G:\server\www\engine\kernel\hook.php:79}() G:\server\www\engine\kernel\hook.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP  22. fn_image_set() G:\server\www\engine\kernel\hook.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP  23. Page->get() G:\server\www\lot\extend\plugin\lot\worker\image\index__.php:7

[28-May-2017 09:17:01 Asia/Jakarta] PHP  24. Page->content() G:\server\www\engine\kernel\page.php:170

[28-May-2017 09:17:01 Asia/Jakarta] PHP  25. Page->__call() G:\server\www\engine\kernel\page.php:170

[28-May-2017 09:17:01 Asia/Jakarta] PHP  26. Page->__get() G:\server\www\engine\kernel\page.php:42

[28-May-2017 09:17:01 Asia/Jakarta] PHP  27. Hook::NS() G:\server\www\engine\kernel\page.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP  28. call_user_func_array:{G:\server\www\engine\kernel\hook.php:94}() G:\server\www\engine\kernel\hook.php:94

[28-May-2017 09:17:01 Asia/Jakarta] PHP  29. Hook::fire() G:\server\www\engine\kernel\hook.php:94

[28-May-2017 09:17:01 Asia/Jakarta] PHP  30. call_user_func_array:{G:\server\www\engine\kernel\hook.php:79}() G:\server\www\engine\kernel\hook.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP  31. fn_block_replace_e() G:\server\www\engine\kernel\hook.php:79

[28-May-2017 09:17:01 Asia/Jakarta] PHP  32. Block::replace() G:\server\www\lot\extend\plugin\lot\worker\block-e\index.php:9

[28-May-2017 09:17:01 Asia/Jakarta] PHP  33. preg_replace_callback() G:\server\www\lot\extend\block\engine\kernel\block.php:73

[28-May-2017 09:17:01 Asia/Jakarta] PHP  34. Block::{closure:G:\server\www\lot\extend\block\engine\kernel\block.php:69-73}() G:\server\www\lot\extend\block\engine\kernel\block.php:73

[28-May-2017 09:17:01 Asia/Jakarta] PHP  35. call_user_func_array:{G:\server\www\lot\extend\block\engine\kernel\block.php:72}() G:\server\www\lot\extend\block\engine\kernel\block.php:72

[28-May-2017 09:17:01 Asia/Jakarta] PHP  36. {closure:G:\server\www\lot\extend\plugin\lot\worker\block-e\index.php:4-9}() G:\server\www\lot\extend\block\engine\kernel\block.php:72

[28-May-2017 09:17:01 Asia/Jakarta] PHP  37. ob_start() G:\server\www\lot\extend\plugin\lot\worker\block-e\index.php:5

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要将 ob_get_clean 的值指定给变量

<?php ob_start(); ?>
<html>
  <head></head>
  <body>
    <?php ob_start(); ?>
    <?php echo '$page->content'; ?>
    <?php $inner = ob_get_clean(); ?>
  </body>
</html>
<?php $outer = ob_get_clean(); ?>
<?php var_dump($inner, $outer); ?>

请注意,我在 $ page-&gt;内容周围加上引号以便于阅读此代码,您不应该使用这些引号。