Edit report at https://bugs.php.net/bug.php?id=62437&edit=1

 ID:                 62437
 Updated by:         m...@php.net
 Reported by:        tero dot tasanen at gmail dot com
 Summary:            Strange behavior with global variables (objects) in
                     ob_start() output callback
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Output Control
 Operating System:   Linux 64bit
 PHP Version:        5.3.14
-Assigned To:        
+Assigned To:        mike
 Block user comment: N
 Private report:     N

 New Comment:

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

So something has to be shut down first, it's kind of a chicken and egg problem.
Object destruction is performed in two stages, objects with e.g. circular 
references are destroyed at the very last (try $object->ref=$object to see the 
difference).


Previous Comments:
------------------------------------------------------------------------
[2012-07-02 11:51:53] tero dot tasanen at gmail dot com

Yes, I agree that there might be two bugs here. 

Here's another test script that shows that this only affects objects and not 
other data types.

http://3v4l.org/Ip5Sn
<?php

function output($buffer) {
  global $object, $string, $int, $float, $array;

  $ret_val = array($string, $object->bar, $int, sprintf("%.2f", $float), 
$array[0]);
  return implode(" ", $ret_val);
}

ob_start('output');

$object = new stdClass();
$object->bar = "bar";

$string = "foo";
$int = 1;
$float = 2.0;
$array = array('array');

// $test = $object;
?>

------------------------------------------------------------------------
[2012-06-30 13:25:09] Sjon at hortensius dot net

Contrary to what tony2001 says in #40604, this was actually working fine until 
it 
was broken in 5.2

http://3v4l.org/SUUkK

The reason that http://3v4l.org/pZ2PP works might be explained to the second 
reference to the same object which could prevent destruction, but that would 
then 
actually be a bug too (since it indicates a memory-leak).

------------------------------------------------------------------------
[2012-06-28 11:56:12] tero dot tasanen at gmail dot com

Description:
------------
Global variables in output buffering seem to work in very strange way. don't 
know 
actually if this has anything to do with output buffering callback but see the 
test case attached to reproduce this. 

And the strangest thing is that if you uncomment the last line the script works 
as expected!

After some searching I found two similar bug reports (#40604, #44840) and the 
comments indicate that this is expected behavior?! Not just that it seem really 
strange that all objects get destroyed before the output callback is called, 
but 
why does the use of the $test variable in the end of the script change this 
behavior? This really does not make any sense! 

Test script:
---------------
<?

function output($buffer) {
  global $object;
  return $buffer . $object->bar;
}


ob_start('output');
$object = new stdClass();
$object->bar = "bar";

echo "foo ";
// $test = $object;


Expected result:
----------------
foo bar

Actual result:
--------------
PHP Notice:  Trying to get property of non-object in /home/ttasanen/test.php on 
line 5
PHP Stack trace:
PHP   1. output() /home/ttasanen/test.php:0
foo 


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=62437&edit=1

Reply via email to