PHP: Execution model and memory model

Source: Internet
Author: User
Background

For any language, it makes sense to understand its execution model and memory model, and the content in this article is not necessarily correct, please criticize it more.

Execution model

Each request is a standalone PHP process, and two requests are completely isolated, and sessions and cookies are shared across multiple requests through other mechanisms, such as static variables and global properties that are reinitialized in each request.

Example

1 
 
   '; 6?>

Results

2//No matter how many requests are refreshed, the result is 2.

Basic rules for memory models

The following are the memory management rules for PHP:

Default is passed by value. An explicit declaration must be passed by reference. The value of the object type is the address of the object.

1 $symbol = value;

Where: Symbo is a symbol, stored in the symbol table (the global symbol table or the active symbol table), value is the values of the symbol reference.

1 $symbol _ref = & $symbol;

Where: Symbo_ref and Symbo L refer to the same value.

First small test

Code

1 
 
   "Ten?>

Results

1 var_a:2, Var_b:1, Var_c:2, Var_d:1, Var_e:2

Memory changes

The first step

Step Two

Step Three

Fourth Step

Fifth Step

Sixth step

A second quiz

Code

1 
 
   value = $value; 7     } 8      9     function GetValue () {         return $this->value;     }12}13 $var _obj = new TestClass (), $var _obj_copy = $var _obj;16 $var _obj_ref = & $var _obj;17 $var _obj_re F->setvalue (2); Echo $var _obj->getvalue (). '
'; Echo $var _obj_copy->getvalue (). '
echo $var _obj_ref->getvalue ().
';?>

Results

1//Output result 2 3 24 25 2

Memory changes

The first step

Step Two

Step Three

Fourth Step

Note

The real PHP memory is not the way it is, but the semantics are no different from what is described above, and PHP does some extra work to optimize memory.

PHP in order to improve the efficiency of memory, not when assigned to the symbol immediately copied by value, each value maintains a reference count (by how many symbols referenced), at some appropriate moment will be automatically copied, but these moments for developers is transparent, can not be considered.

Friends who are interested, can refer to this article: http://www.laruence.com/2008/09/19/520.html.

  • Related Article

    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.