Not much to say, code is the true.
First paragraph:
Class globals{
Public $var = Array ();
function __construct () {
Global $_fanwe;
$_fanwe=2;
}
}
New Globals ();
function Nice () {
Global $_fanwe;
Echo $_fanwe;
}
Nice ();
Input:2
Second paragraph:
function Globals () {
Global $_fanwe;
$_fanwe=2;
}
function Text () {
Global $_fanwe;
Echo $_fanwe;
}
Globals ();
Text ();
Input:2
The above two files were tested separately, and the results were still 2. The reason is that the global variable has already registered the global variable when PHP compiles and stops in PHP memory until the PHP process is finished, and the PHP garbage collection mechanism destroys the global variable.
PHP at compile time, when a defined function is called, PHP will give the function to allocate a certain amount of memory space and a certain amount of resources as a condition to execute the function, the space given to the function is nothing more than a stored variable and function execution process need to return the results, anyway, this part of the space as a PHP process inside the space, So, when a global variable is defined within this function, the global variable will be put into the memory allocated by the function, although the global variable defined in the function can be used as a real global variable by the rest of the function because it stops in memory. Note that this can be used only if you have to make the global variable stop in memory before you use it, that is, you can use this global variable by calling the function that contains the global variable (this is the global variable that stops in memory). To verify, take a look at the following code and its output:
function Globals () {
Global $_fanwe;
$_fanwe=2;
}
function Text () {
Global $_fanwe;
Echo $_fanwe;
}
Text ();
In fact, this function does not have any output, because in the text $_fanwe is not assigned, you may be asked in Globals this function is not already defined $_fanwe, is the global variable and also assigned value? Why hasn't it been exported yet? Comparing the previous program, you can see that there is no call to the Globals () function: No call, no natural memory, where do you output?
Say so much, in fact, the same class, a class is not instantiated before is just a fictitious definition, does not exist in memory, once instantiated this class, PHP will give this class to allocate memory, stop in memory, until the end of the PHP process or in the PHP process to end the class, this global variable disappears , the truth is the same as the above function.
Alas, this is because of the study of some of the framework of the feeling and hair ...
The above describes the PHP global variable memory management, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.