I'm writing a logging tool which writes messages to a file, and I'm not sure of the best way to deal with the file pointer. I'm tossing up between these two methods:
// Open, Write, Close; Open, Write, Close...
function write($message) {
$fh = fopen('file.log', 'a');
fwrite($fh, $message . "\n");
fclose($fh);
}
// OR -----
// Open, Write, Write, Write..., Close
function __construct() {
$this->fh = fopen('file.log', 'a');
}
function __destruct() {
fclose($this->fh);
}
function write($message) {
fwrite($fh, $message . "\n");
}
I imagine that this class will be loaded and constructed on every page, but not necessarily used, though it most likely will be.
Are there are performance, security or miscellaneous pitfalls of either method, and which would you recommend?