|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-04-22 05:19 UTC] rd dot contact at free dot fr
Description:
------------
During the __destruct, when using the global var assigned to the object, php crashes in some cases.
Reproduce code:
---------------
class test{
function __destruct (){
//$GLOBALS['p']=$this; // <- this crash apache for all tests
//$GLOBALS['p']++; // <- this crash apache for all tests
print_r($GLOBALS['p']); // <- crash only test 1. test 2 => "Undefined variable p"
}
}
$p=new test;
//test 1
$p=1; // crash apache (as $p=null, $p='bug', $p=new test ...)
//but,
//test 2
//unset($p); // no crash: "Undefined variable: p on line 5"
Expected result:
----------------
No crash, and a way to use the global var during destruct.
(so destruct could call external functions that use the global var of the object. Why not unregister the global var after the destruct call ?)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 15:00:01 2025 UTC |
/* -- TEST 1 I use the global var instead of "$this" to demonstrate that calling it will segfault php. Try TEST2 to see why it could be useful to use the global var during the destruct. unset($p) instead of $p=[anything], don't crash. */ class test{ public $c=1; function __destruct (){ $GLOBALS['p']->c++; // no warning print $GLOBALS['p']->c; // segfault } } $p=new test; $p=null; //destroy the object by a new assignment (segfault) //---CUT---- /* -- TEST 2 More realistic example: During the destruct, I need to call an external function that uses the global var of the object. calling a methode: works incrementing a property: segfault */ function dbug($msg){ $GLOBALS['p']->printmsg($msg); // works $GLOBALS['p']->c++; // segfault } class test{ public $c=1; function __destruct (){dbug('Destruct');} function printmsg($msg){print $msg;} } $p=new test; $p=null; //destroy the object by a new assignment (segfault)