tags:

views:

251

answers:

3
class MyDestructableClass {
   function __construct() {
       print "\nIn constructor\n";
       $this->name = "MyDestructableClass";
   }

   function __destruct() {
       print "\nDestroying " . $this->name . "\n";
   }
}

$obj = new MyDestructableClass();

When the above script is in a complex environment,the __destruct won't get called when exit,but I can't reproduce it easily.Have someone ever noticed this ?

EDIT

I'll post the whole stuff here,it's the testing environment of symfony,which means you can easily reproduce it if you are familar with the framework:

require_once dirname(__FILE__).'/../bootstrap/Doctrine.php';


$profiler = new Doctrine_Connection_Profiler();

$conn = Doctrine_Manager::connection();
$conn->setListener($profiler);

$t = new lime_test(0, new lime_output_color());

class MyDestructableClass {
   function __construct() {
       print "\nIn constructor\n";
       $this->name = "MyDestructableClass";
   }

   function __destruct() {
       print "\nDestroying " . $this->name . "\n";
   }
}

$obj = new MyDestructableClass();
$news = new News();

$news->setUrl('http://test');
$news->setHash('http://test');
$news->setTitle('http://test');
$news->setSummarize('http://test');
$news->setAccountId(1);
$news->setCategoryId(1);
$news->setThumbnail('http://test');
$news->setCreatedAt(date('Y-m-d H:i:s',time()));
$news->setUpdatedAt(date('Y-m-d H:i:s',time()));
$news->save();
exit();
A: 

Don't familiar with the Doctrine, but check one point: check for possible exceptions in _construct()/_destruct() they can produce fatal errors.

Shein Alexey
Will `_destruct` get called when there is a fatal error?
if you have fatal error any execution stops immediately, so it can be hard to find the place it happened.
Shein Alexey
+1  A: 

Not having an output on the screen doesn't mean the destructor is not called : the ouptut could be captured using output_buffering (maybe lime does that, to be able to work on it ? ), and not echoed when the script ends, for instance.

For testing purposes, you could try writing to a file, in your __destruct method, instead of just echoing some text.
(Just make sure your application / PHP has the required privileges to write to your destination file)

(I've already run into situations where I would not see the output made in a destructor -- but it was actually called)

Pascal MARTIN
As far as I know even the database connections are closed when __destruct is called.
Hippo
I tested with `file_put_contents` to ensure it's not called. But there is a `PHP Fatal error`
What does the Fatal Error say ? Does it indicate a problem in your script, or something not working "as expected" ?
Pascal MARTIN
+1  A: 

The __destruct will not be called:

  • If "exit" is called in another destructor
  • Depending on the PHP Version: If "exit" is called in a "register_shutdown_function" function
  • If there is a FATAL error somewhere in the code
  • If there is an exception thrown in another destructor
  • If you try to HANDLE an exception in a destructor ( PHP >= 5.3.0

Guess thats all i can think of right now

& What Pascal MARTIN said. Thats the first step of debugging that.

edorian