I have found that it is impossible to write (file_put_contents
or simple fwrite
) inside __destruct()
of PHP class, how to invoke that? full function:
function __destruct()
{
foreach($this->data as $name=>$value) $$name=$value;
if(count($this->modules)>0)
{ foreach($this->modules as $name=>$value)
{
ob_start();
include $value;
$content=ob_get_contents();
ob_end_clean();
$$name = $content;
}
}
ob_start();
include $this->way;
$content = ob_get_contents();
ob_end_clean();
$fp = fopen('cache.txt', 'w+');
fputs($fp, $content);
fclose($fp);
echo $content;
}
It's possible the problem you have is that you're still referencing the object inside of your destruct().
You should have no problems writing to a file in a __destruct at all. The below example proves it:
<?php
class TestDestruct{
function __construct(){
$this->f = 'test';
}
function __destruct(){
print 'firing';
$fp = fopen('test.txt', 'w+');
fputs($fp, 'test');
fclose($fp);
}
}
$n = new TestDestruct();
empty($n);
Remember the destruct will only be fired once there are no references to that object. so if you were to be doing something like
fputs($fp, $this->f)
then it's not going to work.