RE: [PEAR] destructor not getting called.
| From: | Matt Giddings | Date: | Mon, 10 Sep 2001 19:57:53 +0000 |
| Subject: | RE: [PEAR] destructor not getting called. | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-311@lists.php.net to get a copy of this message | ||
I do call $this->PEAR() within the constructor and the thing still doesn't
work. Whats supposed to happen is all the updated data is supposed to be
written to a file when the destructor is called, this doesn't happen unless
I explicity call the destructor myself. Heres the code I'm using, which is
pretty much the example given within the pear documentation.
------8< start of example code >8------
<?php
require_once "/home/httpd/html/monitorsugar.com/htdocs/inc/pear/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.
?>
------8< end of example code >8------
-----Original Message-----
From: Tomas V.V.Cox [mailto:cox@idecnet.com]
Sent: Saturday, September 08, 2001 12:53 PM
To: pear-general@lists.php.net; Matt Giddings
Subject: Re: [PEAR] destructor not getting called.
Matt Giddings wrote:
>
> register_shutdown_function("_PEAR_call_destructors"); is located at the
end
> of PEAR.php and based on the code, when php shuts down it should call
> _PEAR_call_destructors. Since the class "filecontainer" is an extention
of
> "PEAR", _filecontainer is supposed to be registered as a destructor based
on
> the call to $this->PEAR() in filcontainer's constructor. So, correct me
if
> I'm wrong, but the way I see it is like this. PHP shuts down ->
> _PEAR_call_destructors is called -> _filecontainer is called. This
doesn't
> appear to be happening. Any ideas why it wouldn't. Are there any
> configuration switches that need to be set inorder to allow this to work?
>
You are right. To register a destructor:
class Foo extends PEAR {
// constructor
function Foo() {
$this->PEAR();
}
// destructor
function _Foo() {
// code to be executed when the
// script ends
}
}
Please note that _Foo() is called under register_shutdown_function() so
no "echo" or "print" statement will be output if you are not using the
php script mode (ie. as apache module). So perhaps your problem is
there.
Tomas V.V.Cox
--
PEAR General Mailing List (http://pear.php.net/)
To unsubscribe, e-mail: pear-general-unsubscribe@lists.php.net
For additional commands, e-mail: pear-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com