<?php function callback($buffer) { // replace all the apples with oranges return (str_replace("apples", "oranges", $buffer)); } ob_start("callback"); ?> <html> <body> <p>It's like comparing apples to oranges.</p> </body> </html> <?php ob_end_flush(); //打印It's like comparing oranges to oranges. ?> /************分割线****************/ <?php class Buffer { var $template = ''; function Buffer() { $this->startBuffer(); } function startBuffer() { ob_start(array(&$this, 'callback')); //PHP中类名和方法名是以键值对的形式存在的.也就是数组 } function callback($buffer) { return $buffer . $this->template; } } $buffer2 = new Buffer(); $buffer2->template = '页面都执行完毕后输出我'; echo '看这里.假如我是个页面.'; ?>