Should know but... blush

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

This is not a problem The $file_handle is NOT a copy of $this->getHandle()
The $file_handle POINTS to the object created/returned by $this->getHandle()
There is only ONLY object, there is no COPY.

This is fundamental concept in OOP - objects are not copied when you assign them to a variable. A new variable basically is given an address space of memory where the original object “lives”

So don’t worry about it.

You code, however, could be simplified a lot by just using
file_put_contents(date('Y-m-d H:i:s ').$this->msg);
With fule_put_contents() you don’t need to create handler, it’s done for you automatically.

I don’t know why you attempting to set the file permission to 755 (executable)? Is this really necessary?

Thanks lampcms.com!

I should know this as this was a change done when php went from php4 to php5, but I appreciate you clarifying this as I had not thought of this in a long time and had forgotten. Like I said, I should have known this!

file_put_contents() looks like a better way… did not know about this method - think I could have read the ‘See also’ in the php manual? Problem is i have been using fopen for a long time, but now I am going to refactor the code.

When using fopen I did need to use chmod to 0755 but interestingly with file_put_contents() I do not have to chmod the file?

Regards,
Steve

file_put_contents does all these things for you. You can also check the return value of file_put_contents() to make sure it was written.

Thanks again!

Steve