5

var_export function causes an exception while argument has circular references. Are there any alternatives (except serialize) which handle it correctly?

Charles
  • 50,010
  • 13
  • 100
  • 141
darja
  • 4,875
  • 9
  • 32
  • 46

3 Answers3

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,159
  • 3
  • 25
  • 32
2

Are you looking for var_dump or even debug_backtrace

Update:

Converting object to string

Community
  • 1
  • 1
Sarfraz
  • 367,681
  • 72
  • 526
  • 573
  • 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,732
  • 1
  • 19
  • 16