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

 ID:                 63539
 Updated by:         larue...@php.net
 Reported by:        cumhuronat at gmail dot com
 Summary:            ob_start output_callback can not access global
                     objects
-Status:             Open
+Status:             Wont fix
 Type:               Bug
 Package:            Output Control
 Operating System:   Centos
 PHP Version:        5.3.18
 Block user comment: N
 Private report:     N

 New Comment:

output buffer callback will be called in php request shutdown phase.

and exactly after calling possible destructors of objects.

because there might be some output in object's destructor. 

that means, while in output buffer callback, the global object anotherClass has 
been destroyed.
you can use following codes:

<?php
class AnotherClass{
    public $world = "world";
}
$anotherClass = new AnotherClass();

class TestClass{
    function __construct(){
        global $anotherClass;
        ob_start(function($input) use($anotherClass) {
            return $input.$anotherClass->world;
        });;
    }
}
$tClass = new TestClass();

echo "hello";


which will inc the refcount of $anotherClass , and prevent it be destroyed 
before output buffer callback be called


Previous Comments:
------------------------------------------------------------------------
[2012-11-16 12:18:30] cumhuronat at gmail dot com

Description:
------------
I'm currently using output buffering for some sort of header & footer 
automatization. But I need to access global variables inside the 
output_callback function. If I don't use class oriented code there isn't any 
issue. 

Test script:
---------------
class AnotherClass{
    public $world = "world";
}
$anotherClass = new AnotherClass();

class TestClass{
    function __construct(){
        ob_start(array($this,"callback"));
    }

    function callback($input){
        global $anotherClass;
        return $input.$anotherClass->world;
    }
}
$tClass = new TestClass();

echo "hello";

Expected result:
----------------
helloworld

Actual result:
--------------
hello


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



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

Reply via email to