Jump to content

__tostring on object Not working when passed to another object !!


lostnucleus

Recommended Posts

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 ??

 

 

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";


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.