Generally speaking, if there are recursive references to PHP objects, memory leaks will occur. This bug has existed in PHP for a long time. Let us reproduce this bug first. The sample code is as follows:
<?php class Foo { function __construct() { $this->bar = new Bar($this); } } class Bar { function __construct($foo) { $this->foo = $foo; } } for ($i = 0; $i < 100; $i++) { $obj = new Foo(); unset($obj); echo memory_get_usage(), "/n"; } ?>
Run the above code, you will find that the memory usage should remain unchanged, but in fact it keeps increasing, and unset does not fully take effect.
A lot of current development is based on frameworks. There are complex object relationships in the application, so you are likely to encounter such problems. Let’s take a look at what expedients can be done:
<?php class Foo { function __construct() { $this->bar = new Bar($this); } function __destruct() { unset($this->bar); } } class Bar { function __construct($foo) { $this->foo = $foo; } } for ($i = 0; $i < 100; $i++) { $obj = new Foo(); $obj->__destruct(); unset($obj); echo memory_get_usage(), "/n"; } ?>
The method was a bit ugly, but it was finally dealt with. Fortunately this bug has been fixed in the CVS code of PHP5.3.
It is necessary to pay attention to this when designing PHP programs! I believe that what is described in this article has certain reference value for everyone's PHP program design.
The function demo has two inevitable output statements, one is the beginning one, and the other is the last one!
Function running process
1. Output num
2. Determine whether num is greater than 0 to determine whether to call the function
3. Output num
When num is greater than 0, each call The function only executes to the second step, and then waits for the second step to call itself to return the result before continuing to execute. Therefore, every time it calls itself and returns, num is output again, that is, the following values are output in reverse order!
Okay, just a few words.
When writing sql statements, it is best to capitalize all mysql functions. This way, the sql statements will be clear at a glance and it will be nice to say.
For example: $sql = "SELECT * FROM `user` WHERE `id` = '$id'";
And the table name and field names all use the dot next to ` 1 (under the English input method Dot) and the variable copied to the field, whether it is a string or a numeric type, should be enclosed in ' single quotes
, which is very beneficial. . . . .