* Thus wrote Mag:
> Hi Curt,
> Thanks for replying.
> 
> > $obSaveFile = new SaveToFile();
> > $obSaveFile->open('/path/to/file.html');
> > ob_start(array(&$obSaveFile, 'save'), 1024);
> > 
> > /* do your stuff  here */
> > 
> > ob_end_flush();
> > $obSaveFile->close();
> 
> 
> Problem is, I have not really learnt classes as yet, I
> know how to use them but I really have no idea as to
> how to make them. Looking at your above code I see you
> are calling open() and close() of the class but not
> write() is that a mistake or just my lack of
> understanding of classes? (No idea what this:
> &$obSaveFile 
> means either)

Oops, yeah the ob_start should have been:
  ob_start(array(&$obSaveFile, 'write'), 1024);

The &$ in $&obSaveFile uses a reference instead of a copy of the
object (an old php4 trick, see more on references)

What will happen is anytime PHP's output buffer decides to write
the contents of the buffer, in this case I told it to do it when
ever 1024 bytes where collected, it will call:

  $obSaveFile->write($contents_of_buffer);

Then your write() class method will write the buffer contents to
the file handle you opened in open().

the ob_end_flush() forces the rest of the buffer (<1024) to get sent too to
your write() class method, ensuring everything is written.


> Also I have heard that outbuffering can be quite
> complicated, would you mind filling in your example a
> bit more and I can make add to it and work from there?

Here is the class in full (w/o error checking):

class SaveToFile {
  var $file_handle;

  function open($file)  {
    $this->file_handle = fopen($file, 'w');
  }
  function write($buf) {
    fwrite($this->file_handle, $buf);
  }
  function close() {
    fclose($this->file_handle);
  }
}

Curt
-- 
Quoth the Raven, "Nevermore."

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to