[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: errorObject id #1/error Can someone help me? I want to print the contents of the $error object as well... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Overloaded Class The __tostring() Method
- Original Message - From: Daniel Schierbeck [EMAIL PROTECTED] To: [EMAIL PROTECTED] 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: errorObject 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... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Overloaded Class The __tostring() Method
John Holmes wrote: - Original Message - From: Daniel Schierbeck [EMAIL PROTECTED] To: [EMAIL PROTECTED] 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: errorObject 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... It's more the principle of automatically building the string... I just used the error thing as an example. What i'd like to do is to be able to create an XML string out of an object, no matter how many levels of properties that object has. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php