96

Possible Duplicate:
PHP ToString() equivalent

how to convert object into string in php

Actually i am dealing with web service APIs.i want to use output of one API as a input for another API. when i am trying to do this i got error like this:Catchable fatal error: Object of class std could not be converted to string in C:\ ...

this is the output of first API::stdClass Object ( [document_number] => 10ba60 ) now i want only that number to use as input for 2nd AP

print_r and _string() both are not working in my case

CalvT
  • 3,123
  • 6
  • 37
  • 54
JJ.
  • 977
  • 1
  • 6
  • 3
  • 3
    do you mean cast it to a string, like you might in C? Or view a direct visual representation of the data it contains? – Cam Mar 18 '10 at 11:03
  • 1
    Actually i am dealing with web service APIs.i want to use output of one API as a input for another API. when i am trying to do this i got error like this:Catchable fatal error: Object of class std could not be converted to string in C:\.... – JJ. Mar 18 '10 at 11:33
  • I think you'll have to give us more details. Error shows that another API uses return from first one as a string. I think it all depends of what information is returned by the first API and what information second API wants. – Tomasz Struczyński Mar 18 '10 at 11:36
  • this is the output of first API::stdClass Object ( [document_number] => 10ba60 ) now i want only that number to use as input for 2nd API – JJ. Mar 18 '10 at 11:44
  • print_r and _string() both are not working in my case – JJ. Mar 18 '10 at 11:51
  • JJ, i added your comments to the question, where they belong. – user187291 Mar 18 '10 at 12:46
  • I use `json_encode()` to covert unpredictable variables, objects into string. It's a quick trick and can be understood on other platforms than PHP – Accountant م Jan 17 '19 at 12:06

5 Answers5

92

You can tailor how your object is represented as a string by implementing a __toString() method in your class, so that when your object is type cast as a string (explicit type cast $str = (string) $myObject;, or automatic echo $myObject) you can control what is included and the string format.

If you only want to display your object's data, the method above would work. If you want to store your object in a session or database, you need to serialize it, so PHP knows how to reconstruct your instance.

Some code to demonstrate the difference:

class MyObject {

  protected $name = 'JJ';

  public function __toString() {
    return "My name is: {$this->name}\n";
  }

}

$obj = new MyObject;

echo $obj;
echo serialize($obj);

Output:

My name is: JJ

O:8:"MyObject":1:{s:7:"*name";s:2:"JJ";}

Community
  • 1
  • 1
Greg K
  • 10,770
  • 10
  • 45
  • 62
39

Use the casting operator (string)$yourObject;

Webleeuw
  • 7,222
  • 33
  • 34
17

You have the print_r function, check docs.

francisco
  • 1,387
  • 2
  • 12
  • 23
remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
9

There is an object serialization module, with the serialize function you can serialize any object.

Wolph
  • 78,177
  • 11
  • 137
  • 148
  • 2
    but if the object has complex datatypes or resources you should work with it using magic functions => http://php.net/manual/en/language.oop5.magic.php like __wakeup or __sleep to serialize deserialize resources or complex datatypes. – markcial Mar 18 '10 at 11:04
  • 1
    According to php.net, when using serialize(), an attempt is made to call __wakeup/__sleep at the appropriate times automatically, so all you have to worry about is actually implementing the functions themselves. My guess though, is that this is a bit beyond what the requirements of the OP would be! – Cam Mar 18 '10 at 11:19
6

In your case, you should simply use

$firstapiOutput->document_number

as the input for the second api.

Tomasz Struczyński
  • 3,273
  • 23
  • 28