【问题标题】:how to start ob_start inside a class?如何在类中启动 ob_start?
【发布时间】:2011-04-25 20:20:40
【问题描述】:

我正在做一些关于从 php 中最小化 html 的研究。喜欢

class themeing
{
    function render( $file, $folder )
    {
        if ( COMPRESS ) {
            // this is the problem
            ob_start('compressor'); 
        }

        $get = VIEWS . $folder . '/' . $file . '.phtml';

        if ( COMPRESS ) {
            ob_end_flush();
        }

        return $get;
    }

    function compressor($buffer)
    {
        $search = array(
            '/<!--(.|\s)*?-->/',
            '/\>[^\S ]+/s',
            '/[^\S ]+\</s',
            '/(\s)+/s'
        );
        $replace = array(
            '',
            '>',
            '<',
            '\\1'
        );

        $buffer = preg_replace($search, $replace, $buffer);
        return $buffer;
    }
}

问题是我如何调用这个 ob_start(function) ?我们可以做 ob_start($this->compresssor()) 吗? (好吧,我知道它失败了)在课堂上?有人吗??

感谢您的关注。

亚当斋月

【问题讨论】:

  • 正确的术语是“缩小”或“缩小”
  • (nitpick) 我不希望 Themeing 类能够缩小。

标签: php ob-start


【解决方案1】:

如果你有静态方法,可以这样访问:

ob_start(array(get_called_class(),'compressor'))

【讨论】:

    【解决方案2】:
    ob_start(array($this,'compressor'))
    

    PHP 使用array(instance,function) representation 将类的成员函数表示为可调用函数。

    【讨论】:

    • 真的很感谢 victor,stackoverflow 开始上瘾了。呵呵。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2023-04-02
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多