Get the App
SLTechnology News&Howtos  ›  Development  › 

How to destroy objects in PHP

Shulou Source: shulou.com Published: 2022-06-03 14:47:52 09月25日 Update

Today, I would like to talk to you about how to destroy objects in PHP. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

The method of destroying an object in PHP: 1, assign the value NULL to the object variable, the syntax "$obj = null;"; 2, use the unset () function to destroy the object, the syntax "unset ($obj);".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of destroying objects in PHP

Method 1: assign NULL to object variables

An object is destroyed when it is not referenced, so it can be assigned a NULL

Example: there is such an object:

Output result:

Assign NULL to the object variable $obj to destroy the object

$obj = null;var_dump ($obj)

Output result:

Method 2: use the unset () function to destroy the object

Example 1:

Answer:

B = $c = $d = $a; by default, four variables point to the same memory, and the object is still used by the other three variables during unset, so the object is not destroyed, so the destructor is triggered offline (memory is automatically freed after the code is executed)

Example 2:

Memory is automatically freed before the code runs because the object has not been referenced by any variable. The answer is to trigger online.

After reading the above, do you have any further understanding of how to destroy objects in PHP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Objects variables functions output memory methods Zhang San content examples or online code answers results syntax three done pointed tutorials time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei Shulou Tech Info Linux Docker