php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44840 objects destroyed on callback functions
Submitted: 2008-04-26 10:53 UTC Modified: 2008-04-28 08:20 UTC
From: fabiovh at gmail dot com Assigned:
Status: Not a bug Package: Output Control
PHP Version: 5.2.5 OS: RHEL5
Private report: No CVE-ID: None
 [2008-04-26 10:53 UTC] fabiovh at gmail dot com
Description:
------------
It is already filed as Bug #39693, but I did not find a way to reopen it or comment on it. I believe it has been marked as bogus in mistake, or at least with not enough pondering about the problem.

I'm sure this wasn't the behavior in earlier versions of PHP 5, since my code just broke with the upgrade, exactly because objects are now destroyed, but it worked fine before.

At least mentioning this new behavior in the docs (on ob_start) would be sensitive. But to me it's waaay weird that everything else works in those callback functions, except objects.

I think it's pretty severe, since this change of behavior breaks compatibility and makes output buffer processing useless for OO applications. I had to change a lot of code - for the worse - to make it compatible.


Reproduce code:
---------------
<?

ob_start('callback');

class Test {
  function method() {
    return 'cow';
  }
}

$test = new Test();

function callback ($content) {
  global $test;
  
  return 'm:'.$test->method();
}

?>


Expected result:
----------------
cow

Actual result:
--------------
[none]

httpd: PHP Fatal error:  Call to a member function method() on a non-object in (...).php on line 16


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-26 19:17 UTC] jani@php.net
This is expected behaviour.
Object destructors are called before flushing the output buffers, so if
your object has refcount == 1 (which is the case with $t2=$t1 commented
out), $t1 is already destroyed when the callback is called.
You do not see the error message, because the error happens in the
function which flushes the buffer, so this is also expected.
 [2008-04-28 08:20 UTC] fabiovh at gmail dot com
You just copied and pasted the other answer without considering my comments. Is this how the PHP community is?

No mentioning of this important change in manual or changelog, just broken code when upgrading. Support for OO programming actually diminishing. Shame.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 22:01:33 2024 UTC