Strange destructor could somebody test please on NIX?
| From: | Marcel Bariou | Date: | Wed, 13 Feb 2002 17:14:02 +0000 |
| Subject: | Strange destructor could somebody test please on NIX? | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-851@lists.php.net to get a copy of this message | ||
---------- Message transmis ----------
Subject: Strange destructor could somebody test please on NIX?
Date: Wed, 13 Feb 2002 17:06:36 +0000
From: Marcel Bariou <brasnah-dev@brasnah.com>
To: pear-general@list.php.net
Hello everybody
I work with REDHAT 7.2, PHP-4.2.0-dev, Apache 3.23 and I want to make an
intensive use of PEAR. So I start to-day and the install is done
I test the first basic example => Example 1. PEAR: emulated destructors
, from this page : http://pear.php.net/manual/en/class.pear.php
And it seems to me than the destructor has a strange behaviour.
I correct the script ($this->file was not initialized), and I add a forced
call to the destructor and some messages, because I was thinking I'm wrong
somewhere ? But where ?
For the forced call the destructor works and can open and write the file, but
for the automatic call It cannot open the file and doesnot work correctly
I give the corrected script according me below . Thanx for your help
Marcel
###############
<?php
require_once "PEAR.php";
class FileContainer extends PEAR
{
var $file = '';
var $contents = '';
var $modified = 0;
function FileContainer($file)
{
$this->PEAR(); // this calls the parent class constructor
$this->file=$file;
$fp = fopen($this->file, "r");
if (!is_resource($fp)) {
return;
}
$data=fread($fp, 2048);
while (!empty($data)) {
$this->contents .= $data;
echo $this->contents."<BR>";
$data=fread($fp, 2048);
}
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()
{
echo "\n DESTRUCTOR CALLED<BR>";
$fp = @fopen("testfile","w");
if ($fp)
{ print"The file exists!<BR>";fclose($fp); }
else
{ print"The file does not exist<BR>"; }
$fp = fopen($this->file, "r");
if (!is_resource($fp)) {
echo "THE FILE IS NOT A RESSOURCE IN DESTRUCTOR<BR>";
return;
}
echo "THE FILE IS A RESSOURCE IN DESTRUCTOR<BR>";
if ($this->modified) {
echo $this->file." MODIFIED\n<BR>";
$fp = fopen($this->file, "w");
echo "FP => ".$fp."<BR>";
if (!is_resource($fp)) {
echo "\n Not a ressource<BR>";
return;
}
fwrite($fp, $this->contents);
fclose($fp);
}
}
}
$fileobj =& new FileContainer("testfile");
$fileobj->append("this is append with the forced call to the
destructor<BR>\n");
$fileobj->_FileContainer();
$fileobj->append("this is append with the Automatic call to the
destructor<BR>\n");
print "It's PEAR GUY WHICH ENDS, now IT WILL CALL DESTRUCTOR NOW
AUTOMATICALLY and tell you about<BR>";
// When the request is done and PHP shuts down, $fileobj's
// "destructor" is called and updates the file on disk.
?>
-------------------------------------------------------