lostnucleus Posted April 27, 2009 Share Posted April 27, 2009 for e.g if class test1 implements __toString so $obj1 = new test1(); echo $obj1; //works fine now consider a class test2 there is a method called setdata inside it public functio setdata($data) { $this->_data = $data; } when i do $obj2 = new test2(); $obj2->setdata($obj1); I got an error saying the passed variable to function setData is an object and connot be converted into the string ?? Link to comment https://forums.phpfreaks.com/topic/155791-__tostring-on-object-not-working-when-passed-to-another-object/ Share on other sites More sharing options...
GingerRobot Posted April 27, 2009 Share Posted April 27, 2009 I think we're going to need to see your source code. This, for example, works fine for me: <?php class class1{ function __toString(){ return "Some string representation"; } } class class2{ private $data; function setData($s){ $this->data = $s; } function getData(){ return $this->data; } } $foo = new class1(); echo $foo."\n"; $bar = new class2(); $bar->setData($foo); echo $bar->getData()."\n"; ?> Link to comment https://forums.phpfreaks.com/topic/155791-__tostring-on-object-not-working-when-passed-to-another-object/#findComment-820175 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.