tags:

views:

53

answers:

4

I found a logging script like this:

/** 
 * Logging class: 
 * - contains lopen and lwrite methods 
 * - lwrite will write message to the log file 
 * - first call of the lwrite will open log file implicitly 
 * - message is written with the following format: hh:mm:ss (script name) message 
 */  
class Logging{  
  // define log file  
  private $log_file = '/tmp/logfile.txt';  
  // define file pointer  
  private $fp = null;  
  // write message to the log file  
  public function lwrite($message){  
    // if file pointer doesn't exist, then open log file  
    if (!$this->fp) $this->lopen();  
    // define script name  
    $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);  
    // define current time  
    $time = date('H:i:s');  
    // write current time, script name and message to the log file  
    fwrite($this->fp, "$time ($script_name) $message\n");  
  }  
  // open log file  
  private function lopen(){  
    // define log file path and name  
    $lfile = $this->log_file;  
    // define the current date (it will be appended to the log file name)  
    $today = date('Y-m-d');  
    // open log file for writing only; place the file pointer at the end of the file  
    // if the file does not exist, attempt to create it  
    $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");  
  }  
} 

I can't see any file closing code here. Is that just fine? If not, at which point would that be appropriate? Or does PHP close the file anyways as soon as the script is completely executed (run through)?

A: 

Closing a file isn't strictly necessary since most C runtimes will close it automatically when the process ends. However, some C runtimes may not flush the data in the cache when doing this, so you are advised to close all files you open in order to avoid data loss.

Ignacio Vazquez-Abrams
PHP closes any open files itself, no need to involve the C runtime...
gnud
A: 

PHP will in fact close the files it's handling after execution of the script.

It's "best practice" to do this explicitly, but it doesn't matter in most circumstances. I suppose if there was an unhanded exception, you might be left with an 'open' file, and it really takes only one line to close anyway – so why not?

arbales
A: 

As people have said, PHP will close the file automatically. However, it is best practice to close the file.

  1. In most languages you have to explicitly close your files, and failure to do so may lead to data loss. So it's a good habit to be in, and helpful if you ever port your application.

  2. Data may not be flushed to the disk until the close happens. If your script runs for any length of time ( granted, as a PHP script it probably won't ) then you might want more explicit control on when things are written. For my "typical" logging application, I open and close the file on each line, so that my log is saved in the event of a crash

  3. The most important, and overlooked: It stops the next person from having to ask this question again! This is a code clarity issue; if you have to ask if it's right, you should consider rewriting it so that it looks right.

Chris Arguin
A: 

The problem isn't so much closing the file, as flushing the output before the file is closed. If a file is simply closed, then, that's it -- it's closed. If there is any remaining data in it's output buffer, then that's potentially lost.

Now, PHP may well "flush and close" when it closes the file, that I don't know. But that's the first thing the springs to mind in terms of issues with not closing a file explicitly.

Will Hartung