4

我正在做一些关于从 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()) 吗?(好吧,我知道它失败了)在课堂上?任何人 ??

感谢您的关注。

亚当斋月

4

2 回答 2

15
ob_start(array($this,'compressor'))

PHP 使用数组(实例,函数)表示将类的成员函数表示为可调用函数。

于 2010-10-15T09:11:27.893 回答
0

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

ob_start(array(get_called_class(),'compressor'))
于 2021-08-06T06:24:34.333 回答