我發現了一個logging script這樣的:我是否必須明確關閉此文件?
/**
* 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!");
}
}
我看不到任何文件,這裏關閉代碼。這很好嗎?如果不是,那麼在什麼時候適合?或者腳本完全執行完畢後,PHP是否會關閉該文件?
PHP自身關閉任何打開的文件,不需要涉及C運行時... – gnud 2009-12-27 17:59:28