note 14251 deleted from language.variables.scope by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:45:32 +0000 |
| Subject: | note 14251 deleted from language.variables.scope by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217938@lists.php.net to get a copy of this message | ||
Note Submitter: danno at wpi dot edu
----
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