How to convert object to string in php

Possible duplicate:
PHP ToString () equivalent

how to convert object to string in php

In fact, I am dealing with a web services API. I want to use the output of one API as input for another API. when I try to do this, I got this error: Catchable fatal error: An object of class std could not be converted to a string in C: \ ...

this is the result of the first API :: stdClass Object ([document_number] => 10ba60) now I want only this number to be used as input for the second AP

print_r and _string () both do not work in my case

+47
php
Mar 18 '10 at 11:00
source share
5 answers

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

If you want to display only object data, the method above will work. If you want to save your object in a session or database, you need to serialize it, so PHP knows how to restore your instance.

Some code showing 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";}

+55
Mar 18
source share

Use the cast operator (string)$yourObject;

+21
Mar 18 '10 at 11:02
source share

you have print_r doc function

+5
Mar 18 '10 at 11:01
source share

There is a module for serializing an object , with serialize you can serialize any object.

+5
Mar 18 '10 at 11:02
source share

In your case, you should just use

 $firstapiOutput->document_number 

as an input for the second api.

+4
Mar 18 '10 at 12:39
source share



All Articles