For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > August 2004 > Error Handling Example From PEAR Manual









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Error Handling Example From PEAR Manual
dpgirago@mdanderson.org

2004-08-27, 3:57 pm

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 the 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com