In object:
$this->__toString(); OR echo $this;
In object, var_dump:
$this->__toString('var_dump');
Print out a different object:
$class->__toString('print_r', $object);
| <?php | |
| class Foo { | |
| /** | |
| * Prints out the contents of the object when used as a string | |
| * | |
| * @return string | |
| */ | |
| function __toString() | |
| { | |
| $args = func_get_args(); | |
| $method = ( ! empty($args)) ? $args[0] : "print_r"; | |
| $data = (isset($args[1])) ? $args[1] : $this; | |
| $output = '<pre>'; | |
| if($method == "var_dump") | |
| { | |
| ob_start(); | |
| var_dump($data); | |
| $output .= ob_get_contents(); | |
| ob_end_clean(); | |
| } | |
| else if($method == "var_export") | |
| { | |
| ob_start(); | |
| var_export($data); | |
| $output .= ob_get_contents(); | |
| ob_end_clean(); | |
| } | |
| else | |
| { | |
| $output .= print_r($data, TRUE); | |
| } | |
| return $output . '</pre>'; | |
| } | |
| } |