php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31607 ob_start with reference to $this fails
Submitted: 2005-01-19 11:07 UTC Modified: 2005-01-19 22:13 UTC
From: denk at us dot es Assigned:
Status: Not a bug Package: Output Control
PHP Version: 4.3.10 OS: Linux
Private report: No CVE-ID: None
 [2005-01-19 11:07 UTC] denk at us dot es
Description:
------------
When executing the following script, output buffering is activated in the constructor of HtmlPage, using a reference to $this. Php seems to create a copy of the object at this moment, as subsequent changes to the object (setting $page->title in the example) are not reflected in the output. This example produces: "bar:Text". However, when we comment the call to ob_start in the constructor and uncomment the call to ob_start using the reference to $page in the main program, the code works as expected, producing "foo:Text".



Reproduce code:
---------------
class HtmlPage {
    var $title;
    function HtmlPage($title='') {
        $this->title=$title;
        // starting ob here don't work
        ob_start(array(&$this,'obuffer'));
    }
    function obuffer($buffer) {
        return ($this->MakePageTop().$buffer);
    }
    function SetHtmlTitle($title) {
        $this->title=$title;
    }
    function MakePageTop() {
      return '<html><body>'.$this->title;
    }
}

  $page=new HtmlPage('bar:');
//  when used here, it works as expected
//  ob_start(array(&$page,'obuffer'));
  echo 'Text<br>';
  $page->title='foo:';

Expected result:
----------------
foo:Text


Actual result:
--------------
bar:Text

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-19 21:05 UTC] eric at footsteps dot nl
This behaviour is by design, for more information:

http://www.php.net/manual/en/language.oop.newref.php

this line 
$page=& new HtmlPage('bar:');
would have the behaviour you seem to expect.
 [2005-01-19 22:13 UTC] sniper@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 12:01:32 2024 UTC