Pavel V.
Pavel V.

Reputation: 2790

Catchable fatal error: Object could not be converted to string - but var_dump says it IS a string

I have a problem that was already asked by a lot of people here: when trying to echo a string, "Catchable fatal error: Object of class Struct could not be converted to string in D:\Programy\XAMP\htdocs\e-history\test.php on line 30". For most related questions, trying var_dump() function was suggested. However, I tried this and everything seems to be OK, except for it throws the error.

My code (testing version):

$place = Struct::factory('gid','lat','lon','radius');

$places = loadPlaces('', 50, 14);
$j = 0;

var_dump($places[$j]->gid);
echo "$places[$j]->gid";

The output of var_dump and echo is as follows:

string(1) "6"
Catchable fatal error: Object of class Struct could not be converted to string in D:\Programy\XAMP\htdocs\e-history\test.php on line 30

Usually there are no problems with $object->value notation, and I don't know why it doesn't work in this case. I had some problems with array/object mismatches, but I'm almost sure it's not this case. Any idea how to solve it?

EDIT: line 30 in my code is: echo "$places[$j]->gid";

Upvotes: 1

Views: 1508

Answers (1)

Sven
Sven

Reputation: 70863

There is a difference between those two lines:

echo "$places[$j]->gid";
echo $places[$j]->gid;

The first one incorrectly tries to access an object inside a string variable. The second one accesses it just like var_dump. There is no need to wrap variables inside double quotes!

The first version should be like this:

echo "{$places[$j]->gid}";

Upvotes: 2

Related Questions