destructor not getting called.
| From: | Matt Giddings | Date: | Thu, 06 Sep 2001 01:27:47 +0000 |
| Subject: | destructor not getting called. | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-301@lists.php.net to get a copy of this message | ||
Hello,
I'm new to pear and I seem to be having a problem with destructors. I'm
using a modified example from the online pear documentation at php.net.
(see code below) In this example a file gets opened and the contents are
loaded into a string variable, then a small string gets appended to the end
of the string and written back to the file when the destructor is called.
If I explicity call the destructor everything works as it should, but if I
don't call it the appended data doesn't get written to the file.
<?php
require_once "PEAR.php";
//class FileContainer extends PEAR
class filecontainer extends PEAR
{
var $file = '';
var $contents = '';
var $modified = 0;
//function FileContainer($file)
function filecontainer($file)
{
$this->PEAR(); // this calls the parent class constructor
$data = "";
$this->file = $file;
$fp = fopen($file, "r");
if (!is_resource($fp)) {
return;
}
$data = fread($fp, 2048);
//while (!empty($data = fread($fp, 2048))) {
while( !empty($data) ) {
$this->contents .= $data;
$data = fread($fp, 2048);
}
fclose($fp);
}
function append($str)
{
echo "<br><br>now in append<br>\n";
echo "\$this->contents = $this->contents<br>\n";
echo "\$this->modified = $this->modified<br>\n";
$this->contents .= $str;
$this->modified++;
echo "now leaving append<br>\n";
echo "\$this->contents = $this->contents<br>\n";
echo "\$this->modified = $this->modified<br>\n";
}
// The "destructor" is named like the constructor
// but with an underscore in front.
//function _FileContainer()
function _filecontainer()
{
if ($this->modified) {
$fp = fopen($this->file, "w");
if (!is_resource($fp)) {
echo "not a resource file";
return;
}
fwrite($fp, $this->contents);
fclose($fp);
}
}
}
//$fileobj =& new FileContainer("testfile");
$fileobj =& new filecontainer("testfile");
$fileobj->append("this ends up at the end of the file\n");
//$fileobj->_filecontainer();
// When the request is done and PHP shuts down, $fileobj's
// "destructor" is called and updates the file on disk.
?>
Thanks,
Matt
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com