You want to convert a PHP object to a string?
if neither var_dump, print_r, var_export, serialize, json_encode nor __toString is exactly what you're after maybe this can help you satisfy your needs.
For PHP 5.3 and above
<?php
$v = (object) array('a' => 1, 'b' => 2, 'c' => 3);
$r = new ReflectionObject($v);
echo $r->getName() .' {' . implode(', ', array_map(
function($p) use ($v) {
$p->setAccessible(true);
return $p->getName() .': '. $p->getValue($v);
}, $r->getProperties())) .'}';
Will output:
stdClass {a: 1, b: 2, c: 3}
For a more conventional approach compatible with previous PHP 5 flavours try
<?php
class ExampleClass {
private $pvt = 'private';
protected $prot = 'protected';
public $pub = 'public';
}
$v = new ExampleClass();
$r = new ReflectionObject($v);
echo $r->getName() ." {\n";
foreach ($r->getProperties() as $p)
if ($p->isPublic())
echo "\tpublic ".$p->getName().': '.$p->getValue($v)."\n";
else
echo "\t".($p->isPrivate()?'private ':'protected ').$p->getName().",\n";
echo "}\n";
Which will print:
ExampleClass {
protected pvt,
protected prot,
public pub: public
}