Hi
In this first example the method setHandle() sets the file handle property. Then in the write() method it calls the setHandle() method. The problem is that in looking at the write() method it is unclear what setHandle() does other than knowing it sets a handle - what does it set?
protected function setHandle(){
$this->fh = fopen($this->file_path, $this->fopen_mode);
chmod($this->file_path, 0755);
if (!is_writable ($this->file_path)) {
$errors( 'file or folder not writeable') ;
return 0;
}
return 1;
}
public function write() {
$this->getHandle();
if (0 == filesize($this->filepath) and $this->filepath != NULL){
fwrite($this->fh, date('Y-m-d H:i:s ').$this->msg);
} else {
fwrite($this->fh, "\
" . date('Y-m-d: H:i:s: ').$this->msg);
}
}
Now in this example, the getHandle() returns the handle and it is clearer what it does. The downside is that the $file_handle variable is a copy of the $this->fh.
protected function getHandle(){
$this->fh = fopen($this->file_path, $this->fopen_mode);
chmod($this->file_path, 0755);
if (!is_writable ($this->file_path)) {
$errors( 'file or folder not writeable') ;
return 0;
}
return $this->fh;
}
public function write() {
$file_handle = $this->getHandle();
if (0 == filesize($this->filepath) and $this->filepath != NULL){
fwrite($file_handle, date('Y-m-d H:i:s ').$this->msg);
} else {
fwrite($file_handle, "\
" . date('Y-m-d: H:i:s: ').$this->msg);
}
}
Thoughts on what is best here?
Regards,
Steve