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

 

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.