Can I close the file by removing the handle?

Am I a little puzzled if I can save the command fcloseby simply disabling the variable that carries the handle?

$handle = fopen($file);
...
fclose($handle);

... // script goes on for a long

Compared with:

$handle = fopen($file);
...
unset($handle);

... // script goes on for a long

Is anyone telling?

+5
source share
4 answers

Thanks to the link counting system introduced with PHP 4 Zend Engine, a resource that no longer has links to it is automatically detected and freed by the garbage collector.

Consider the consequences of this. It is safe to assume that all traces of the variable disappeared after garbage collection. In other words, at the end of PHP execution, if PHP is not yet tracking the link, how can I close it? So it seems pretty logical that he will close it when the garbage collector eats it.

, , , , PHP , .


, PHP , . - , . , fclose. .

, .


script:

<?php

$db = mysql_connect(...);

if ($db) {
    echo "Connected\n";
    sleep(5); //netstat during this just for paranoia
    unset($db);
    echo "Unset\n";
    sleep(5); //netstat during this and the connection is closed
}

Windows 7, Debian 6 .

, , , PHP . ..:).


PHP

+3

PHP docs , "", , .

:

$f = fopen("test.php", "r");
if (!flock($f, LOCK_EX)) {
  print("still locked\n");
  exit;
}
unset($f);
sleep(5);
print("goodbye\n");

( test.php, , , fopen() - )

script 5 ; " ", , -, . " ", , -, , , .

+2

unset($handle) $handle, , $handle. fclose(), .

+1

:

fclose $handle resource(5) of type (Unknown)

unset NULL.

fclose php 88 .

: =)

0

All Articles