Code Comments
Programming Forum and web based access to our favorite programming groups.Howdy,
I'm brand new to playing with PEAR, and I'm not sure if this is the
correct list to post this to, but I think there is an error in example
13-1 of the PEAR manual concerning "emulated destructors":
Code from the manual (with added line and comments in red):
require_once "PEAR.php";
class FileContainer extends PEAR
{
var $file = '';
var $contents = '';
var $modified = 0;
function FileContainer($file)// $file contains the string, "testfile" from t
he instantiation below
{ //
but has not yet been assigned to the member attribute, $file...
$this->file = $file; // otherwise fopen call in
destructor returns false...
$this->PEAR(); // this calls the parent class constructor
$fp = fopen($file, "r");
if (!is_resource($fp)) {
return;
}
while ($data = fread($fp, 2048)) {
$this->contents .= $data;
}
fclose($fp);
}
function append($str)
{
$this->contents .= $str;
$this->modified++;
}
// The "destructor" is named like the constructor
// but with an underscore in front.
function _FileContainer()
{
if ($this->modified) {
$fp = fopen($this->file, "w"); // otherwise, $this->file is empty...
if (!is_resource($fp)) {
return;
}
fwrite($fp, $this->contents);
fclose($fp);
}
}
}
$fileobj =& new FileContainer("testfile");
$fileobj->append("this ends up at the end of the file\n");
// When the request is done and PHP shuts down, $fileobj's
// "destructor" is called and updates the file on disk.
Hope this can be passed along to the right folks. Since I've just
installed PEAR, and the above was my test case for whether it was working
correctly, it took some time to realize that it wasn't my setup but the
example code that was broken.
dave
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.