|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 13 05:00:01 2025 UTC |
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