note 14251 modified in language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:45:07 +0000 |
| Subject: | note 14251 modified in language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155592@lists.php.net to get a copy of this message | ||
WARNING! If you create a local variable in a function and then within that function assign it to a
global variable by reference the object will be destroyed when the function exits and the global var
will contain NOTHING! This main sound obvious but it can be quite tricky you have a large script
(like a phpgtk-based gui app ;-) ).
example:
<?php
function foo ()
{
global $testvar;
$localvar = new Object ();
$testvar = &$localvar;
}
foo ();
print_r ($testvar); // produces NOTHING!!!!
?>
hope this helps someone before they lose all their hair
--was--
WARNING! If you create a local variable in a function and then within that function assign it to a
global variable by reference the object will be destroyed when the function exits and the global var
will contain NOTHING! This main sound obvious but it can be quite tricky you have a large script
(like a phpgtk-based gui app ;-) ).
example:
function foo ()
{
global $testvar;
$localvar = new Object ();
$testvar = &$localvar;
}
foo ();
print_r ($testvar); // produces NOTHING!!!!
hope this helps someone before they lose all their hair
http://php.net/manual/en/language.variables.scope.php