meder omuraliev
meder omuraliev

Reputation: 186562

Why would someone use ob_start in this manner and what is the point?

Something is failing in the class I copied over. It's not my class, but the relevant bit that fails is:

class foo {
  function process() {
    ob_start( array( &$this, 'parseTemplate' ) );
  }

  function parseTemplate(){}

}

Does anyone know what the ob_start expression is supposed to do? Call the parse_template method in the context of a copy of &$this? PHP Version is 5.3.2-1. I suspect that the class was coded for 5.0-5.2 and it breaks in 5.3? or could it be something else?

Upvotes: 2

Views: 262

Answers (3)

profitphp
profitphp

Reputation: 8334

ob_start() is output buffering, the parameter passed in is supposed to be a callback that gets called when the buffer is flushed with ob_flush(), ob_clean() or similar function.

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));

Upvotes: 2

Tony L.
Tony L.

Reputation: 7978

Without knowing what is the output of the fail I can guess 2 things. In version 5.3.* there is no need for referencing instances so &$this to just $this. The other thing would be that the ob_start ... needs to be called before any buffer output as far as I know.

Upvotes: 1

Powerlord
Powerlord

Reputation: 88786

The first argument to ob_start is a callback.

To understand what this does, you have to check PHP's definition of callback.

Specifically, it says

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

So, what this actually does is call $this->parseTemplate(); when output buffering is complete.

I'm not sure that the reference operator & is needed here, though.

Upvotes: 4

Related Questions