开发者

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

开发者 https://www.devze.com 2023-02-03 03:16 出处:网络
Something is failing in the class I copied over. It\'s not my class, but the relevant bit that fails is:

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( ar开发者_运维知识库ray( &$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?


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.


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'));


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消