Home >Backend Development >PHP Problem >How to delete variables in php

How to delete variables in php

王林
王林Original
2019-09-21 11:49:214510browse

How to delete variables in php

The destruction of PHP variables or objects can be divided into explicit destruction and implicit destruction:

1. Explicit destruction, when the object is not referenced will be destroyed, so we can unset or assign NULL to it;

2. Implicit destruction, PHP is a scripting language, when the last line of code is executed, all applied memory All must be released;

From the above two destruction methods, we can summarize three methods of destruction: that is

1, unset()

2. $varname=null

3. Destructor __destruct()

For example:

class Human { 
  public $name = '张三'; 
  public $gender = null; 
  public function __destruct() { 
      echo &#39;死了!<br />&#39;; 
  } 
} 
$a = new Human(); 
$b = $c = $d = $a;
unset($a);
$d=null;


echo &#39;<hr />&#39;;
var_dump($a);
echo &#39;<hr />&#39;;
var_dump($b);
echo &#39;<hr />&#39;;
var_dump($c);
echo &#39;<hr />&#39;;
var_dump($d);

结果如下:
Notice: Undefined variable: a in /Library/WebServer/Documents/test.php on line 42
NULL
object(Human)#1 (2) { ["name"]=> string(6) "张三" ["gender"]=> NULL }
object(Human)#1 (2) { ["name"]=> string(6) "张三" ["gender"]=> NULL }
NULL 死了!

First of all, we need to know that in PHP, the variable name is stored in the memory stack. It points to the address of the specific memory in the heap. We can find the memory in the heap through the variable name; therefore we can conclude Conclusion:

<?php
        $a = 1;
        $b = &$a;
        unset($a);
        var_dump($a);
        var_dump($b);

The result is:

Notice: Undefined variable: a in E:\amp\apache\htdocs\index.php on line 5
NULL int(1)

So, unset() does not really destroy the memory value in the variable, it just cuts off the relationship between the variable and the memory. , and the variable name is also removed, but the memory will not be released as long as it is still referenced; in PHP, the value of an object is passed by reference by default, which also explains that in the Human class, $a is unset( ), but $b =$c = $d still has value.

2. $varname=null, the variable name still exists, but the memory value is deleted. So what about in the case of pass-by-reference? Example:

  <?php
        $a = 1;
        $b = &$a;
        $a=null;
        var_dump($a);
        var_dump($b);
输出的结果是:
NULL NULL

So, $varname=null, although the variable name and memory pointer still exist, the value in the memory is completely deleted.

3. As can be seen from the above example, the destructor __destruct() is started when PHP finishes executing the last piece of code, but this understanding is not accurate enough. Example

<?php 
class Human{       
     public  $name = &#39;开始&#39;;        
     public function __destruct(){                
     echo &#39;结束&#39;;        
     }}
     $a = new Human;
     echo $a->name;
     unset($a);   //销毁函数 
     $a = new Human; 
     echo &#39;***********************&#39;;

输出的结果是:

开始结束***************************结束
class Human{       
     public  $name = &#39;开始&#39;;        
     public function __destruct(){                
        echo &#39;结束&#39;;        
     }}
     $a = new Human;
     $b = $a;
     echo $a->name;
     unset($a);   //销毁函数 
     $a = new Human; 
     echo &#39;***************************&#39;;

结果是:

开始***************************结束结束

The above results show that the code does not start when the last line is executed, but is automatically executed when the object is destroyed. The reason why the destructor __destruct() is still executed at the end when the human() function also has unset() is because the memory of the object is not canceled and the object is not completely destroyed when passing by reference.

So the conclusion is: if there is no manual destruction, after the code execution is completed, the system will automatically execute the destructor __destruct() when the memory is released. If the object is destroyed, it will be executed automatically. Destructor.

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to delete variables in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn