Overloaded Class & The __tostring() Method
| From: | Daniel Schierbeck | Date: | Fri, 20 Aug 2004 14:47:25 +0000 |
| Subject: | Overloaded Class & The __tostring() Method | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-194333@lists.php.net to get a copy of this message | ||
I am trying to build an XML-formatted string from an object. I use this code:
<?php
class XML_Object
{
private $childs = array();
public function __get ($prop)
{
if (isset($this->childs[$prop])) {
return $this->childs[$prop];
} else {
trigger_error("Property '$prop' not defined.", E_USER_WARNING);
}
}
public function __set ($prop, $val)
{
$this->childs[$prop] = $val;
}
public function __tostring ()
{
$re = "";
foreach ($this->childs as $key => $val) {
$re .= "<$key>$val</$key>";
}
return $re;
}
}
$error = new XML_Object;
$error->no = 2;
$error->str = "Blablabla";
$error->file = "a_file.php";
$error->line = 54;
$xml = new XML_Object;
$xml->error = $error;
echo $xml;
?>
But i only get this response:
<error>Object id #1</error>
Can someone help me? I want to print the contents of the $error object as well...