php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63539 ob_start output_callback can not access global objects
Submitted: 2012-11-16 12:18 UTC Modified: 2012-11-21 03:48 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: cumhuronat at gmail dot com Assigned:
Status: Wont fix Package: Output Control
PHP Version: 5.3.18 OS: Centos
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cumhuronat at gmail dot com
New email:
PHP Version: OS:

 

 [2012-11-16 12:18 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-21 03:48 UTC] laruence@php.net
-Status: Open +Status: Wont fix
 [2012-11-21 03:48 UTC] laruence@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 09:01:27 2024 UTC