How to destroy an object?

后端 未结 6 1106
挽巷
挽巷 2020-11-29 01:54

As far as I know (which is very little) , there are two ways, given:

$var = new object()

Then:

// Method 1: Set to null
$va         


        
6条回答
  •  一生所求
    2020-11-29 02:43

    May be in a situation where you are creating a new mysqli object.

    $MyConnection = new mysqli($hn, $un, $pw, $db);

    but even after you close the object

    $MyConnection->close();

    if you will use print_r() to check the contents of $MyConnection, you will get an error as below:

    Error:
    mysqli Object
    
    Warning: print_r(): Property access is not allowed yet in /path/to/program on line ..
    ( [affected_rows] => [client_info] => [client_version] =>.................)
    

    in which case you can't use unlink() because unlink() will require a path name string but in this case $MyConnection is an Object.

    So you have another choice of setting its value to null:

    $MyConnection = null;

    now things go right, as you have expected. You don't have any content inside the variable $MyConnection as well as you already cleaned up the mysqli Object.

    It's a recommended practice to close the Object before setting the value of your variable to null.

提交回复
热议问题