Catchable fatal error: Object of class stdClass could not be converted to string

Problem is in this: echo $village_id. "-" .$wg_village; echo $village_id. "-" .$wg_village; It returns:

Catchable fatal error: Object of class stdClass could not be converted to string in /files/function_20.php on line 70

And on line 70 I posted the code that is there, function works perfectly, just returns blank page with an error.

What's wrong with that line? And how can I correct that?

EDIT: A var_dump $village_id and $wg_village :

string(5) "80784"
object(stdClass)#25 (35) {
  ["id"]=> string(5) "80784"
  ["name"]=> string(7) "NewName"
  ["x"]=> string(3) "-99"
  ["y"]=> string(3) "-14"
  ["kind_id"]=> string(1) "5"
  ["user_id"]=> string(1) "1"
  ["rs1"]=> int(36000) ["rs2"]=> int(36000)
  ["rs3"]=> int(36000) ["rs4"]=> int(36000)
  ["workers"]=> string(2) "51"
  ["troop_keep"]=> string(1) "0"
  ["time_update_rs1"]=> string(19) "2014-05-09 01:45:30"
  ["time_update_rs2"]=> string(19) "2014-05-09 01:45:30"
  ["time_update_rs3"]=> string(19) "2014-05-09 01:45:30"
  ["time_update_rs4"]=> string(19) "2014-05-09 01:45:30"
  ["nation_id"]=> string(1) "2" ["merchant_underaway"]=> string(1) "0"
  ["child_id"]=> string(0) "" ["cp"]=> string(3) "104"
  ["cpupdate_time"]=> string(19) "2014-05-08 21:49:55"
  ["krs1"]=> string(1) "1"
  ["krs2"]=> string(1) "1"
  ["krs3"]=> string(1) "1"
  ["krs4"]=> string(1) "1"
  ["faith"]=> string(1) "3"
  ["faith_time"]=> string(19) "2014-05-07 22:05:44"
  ["dateCreate_vila"]=> string(19) "2014-05-05 20:38:26"
  ["capa123"]=> int(36000)
  ["capa4"]=> int(36000)
  ["speedIncreaseRS1"]=> float(1000)
  ["speedIncreaseRS2"]=> float(600)
  ["speedIncreaseRS3"]=> float(800)
  ["speedIncreaseRS4"]=> float(1200)
  ["speedIncreaseRS4Real"]=> float(1149)
}

If you're referencing an object you can't treat it as a string . Try dumping it out and see if you can reference a property inside

 var_dump($village_id);
 var_dump($wg_village);

So now that we have a dump, let's try to access those properties

 echo $village_id. "-" .$wg_village->name;

The only reason that should fail is if the property is protected or private , but this looks like a database result set.

You have to reference the properties using the -> operator. You cannot call them directly. This thread describes that in more detail.


First, you are trying to echo the contents of an object. So first do the following to see what's in $village_id as well as $wg_village . You just used var_dump but I prefer to use print_r with <pre> tags for readability:

echo '<pre>'; 
print_r($village_id);
echo '</pre>'; 

echo '<pre>'; 
print_r($wg_village);
echo '</pre>'; 

So you can try that as well to make things clearer. But from what I am reading, it seems like $village_id is a string with the value of 80784 while $wg_village is an object. Now the thing is, what exactly do you want to get from the $wg_village object?

You would need to access that data like this:

echo $wg_village->id;
echo $wg_village->name;
echo $wg_village->x;
echo $wg_village->y;
etc…
链接地址: http://www.djcxy.com/p/58150.html

上一篇: 为什么在php中的一行代码中会有多个对象操作符?

下一篇: 可捕获致命错误:类stdClass的对象无法转换为字符串