Memory Management for PHP global variables

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.