PHP: execution model and memory model

Source: Internet
Author: User
PHP: execution model and memory model background

It makes sense to understand the execution model and memory model of any language. the content in this article is not correct. Please criticize it more.

Execution model

Each request is an independent PHP process, and the two requests are completely isolated. sessions and cookies share data between multiple requests through other mechanisms, static variables and Global attributes will be reinitialized in each request.

Example

1 
 ';6 ?>

Result

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

Basic memory model rules

The following are the PHP memory management rules:

Pass by value by default. The statement must be passed by reference explicitly. The object type value is the object address.

1 $symbol = value;

Here, symbo is a symbol and is stored in the symbol table (global symbol table or activity symbol table). value is the value referenced by the symbol.

1 $symbol_ref = &$symbol;

Here, symbo_ref and symbo will reference the same value.

First small test

Code

 1 
 "10 11 ?>

Result

1 var_a:2, var_b:1, var_c:2, var_d:1, var_e:2

Memory change

Step 1

Step 2

Step 3

Step 4

Step 5

Step 6

Second small test

Code

 1 
 value = $value; 7     } 8      9     function getValue() {10         return $this->value;    11     }12 }13 14 $var_obj = new TestClass();15 $var_obj_copy = $var_obj;16 $var_obj_ref = &$var_obj;17 18 $var_obj_ref->setValue(2);19 20 echo $var_obj->getValue().'
';21 echo $var_obj_copy->getValue().'
';22 echo $var_obj_ref->getValue().'
';23 ?>

Result

1 // output result 2 3 24 25 2

Memory change

Step 1

Step 2

Step 3

Step 4

Remarks

The real PHP memory is not the same as above, but the semantics is no different from the above description. PHP has done some extra work to optimize the memory.

To improve memory utilization efficiency, PHP does not immediately copy values when assigning values to symbols. each value maintains a reference count (the number of symbols referenced ), it will be automatically copied at some appropriate time, but these moments are transparent to developers and don't need to be considered.

For more information, see http://www.laruence.com/2008/09/19/520.html.

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.