Adam Ramadhan
Adam Ramadhan

Reputation: 22810

how to start ob_start inside a class?

im doing a little reasearch about minimalizing the html from php. like

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;
    }
}

the problem is how do i call this ob_start(function) ? can we do like ob_start($this->compresssor()) ? ( ok i know it fails ) inside a class? anyone ??

Thanks for looking in.

Adam Ramadhan

Upvotes: 4

Views: 3632

Answers (2)

Abraham
Abraham

Reputation: 9875

If you have a static method, it can be accessed like this:

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

Upvotes: 0

Victor Nicollet
Victor Nicollet

Reputation: 24577

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

PHP uses an array(instance,function) representation for representing member functions of classes as callable functions.

Upvotes: 15

Related Questions