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 Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
35 - 26 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 28 04:03:33 2021 UTC