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.