Re: Overloaded Class & The __tostring() Method

From: Date: Fri, 20 Aug 2004 15:07:01 +0000
Subject: Re: Overloaded Class & The __tostring() Method
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-194336@lists.php.net to get a copy of this message
----- Original Message ----- From: "Daniel Schierbeck" <dasch@ulmail.net> To: <php-general@lists.php.net> Sent: Friday, August 20, 2004 10:47 AM Subject: [PHP] Overloaded Class & The __tostring() Method
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...
echo $xml->error->no; echo $xml->error->str; etc... Not sure if that's exactly what you're after, though... ---John Holmes...

« previous php.general (#194336) next »