var_export
function causes an exception while argument has circular references. Are there any alternatives (except serialize
) which handle it correctly?
Asked
Active
Viewed 1,667 times
3 Answers
3
You could try this :
ob_start();
var_dump($var);
$dump = ob_get_contents();
ob_end_clean();
And why can't you use serialize ?

DuoSRX
- 4,179
- 3
- 26
- 32
-
I need such converting for logs. And serialize output is too ugly for logs. – darja Apr 21 '10 at 13:16
2
-
I want to convert object to string. var_dump outputs it, I don't need this – darja Apr 21 '10 at 12:40
-
@darja: The only other way i know of is serializing it (although you say except it). See my updated answer as well. – Sarfraz Apr 21 '10 at 12:46
-
Thanks for link. It sounds that I have two variants - serialize (which I don't want) and json_encode. – darja Apr 21 '10 at 13:50
0
This worked for me:
$backtrace = array_slice( debug_backtrace( 0 ), 0, 6);
$export = var_export( $backtrace, TRUE );
You might need to tweak the slice amount so that it cuts out the circular reference. I had this issue while trying to do a var_export() on an exception while running a PHPUnit test.

2upmedia
- 2,832
- 1
- 20
- 16