tags:

views:

35

answers:

2
$fooinstance = new foo();
/*do something*/
exit('bar');

class foo{
  __destruct(){
    //get the exit message ("bar") to do something with it
  }
}

Hello,

I would like to get the exit message to do something with it (for example insert the exit status in a database). Is there a way to do that ?

Thanks

+1  A: 

The text exit sends isn't special; it's just text that's output before the script dies.

You could get the text with output buffering, though I'm not sure it'll be useful:

<?php
$fooinstance = new foo();
ob_start();
exit('bar');

class foo{
  function __destruct(){
    $c = ob_get_contents(); //$c is "bar"
  }
}

It would probably be best to wrap the exit instruction in a function that did the appropriate logging.

Artefacto
Oh my god......
hsz
This is correct... it enables to both get the exit message (with all the output that happened before it) and echo everything after the ob_get_contents() to the normal output.Yep, I also need some data outputted to the normal output, and at the same time get the die() message...This method can take a lot of memory depending on the string outputted.
Cedric
A: 

This is an incorrect way to do things. Why do you need this particular solution?

FractalizeR
the class I'm writing uses another one that has 'die("foobar")' when there's something wrong. I want to generate a proper report to say why it fails.
Cedric
You need to replace this die() in that class with something like "throw new Exception('Error reason');" and just catch this exception in your outer class. http://us.php.net/exceptions.
FractalizeR
Yep, that's the proper way. I should blast people who uses "die()" in their class, especially as they are essential to the program to run ok.
Cedric
But I guess an other way would be to fork the use of the other class in another process, and in a way get its output : string buffering, put the buffered string in a file and get the content of the file with the parent process or something like that.
Cedric
Thanks for your answer
Cedric
Forking processes from PHP only to get result from another script is an another bad idea, sorry ;)
FractalizeR