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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5032687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:35:26  来源:igfitidea点击:

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

phpstdclass

提问by user464180

I get the following dump & error when running the attached code. What I'm confused by is that $procID appears to be returned as a string, but as soon as I attempt to pass it again, its an object? How do I get it to be/stay a string? Thanks.

运行附加代码时出现以下转储和错误。我感到困惑的是 $procID 似乎作为字符串返回,但是一旦我尝试再次传递它,它就是一个对象?我如何让它成为/保持一个字符串?谢谢。

object(stdClass)#2 (1) {
["processId"]=> string(13)
"Genesis114001" }  string(311)
"Genesis114001" string(293) " Genesis
" Catchable fatal error: Object of
class stdClass could not be converted
to string in
C:\wamp\www\SugarCE\testSOAPShawn.php
on line 15
<?php
set_time_limit(0);
require_once('nusoap.php');
require_once('BenefitSOAP.php');  //WSDL to PHP Classes
$client = new SoapClient('C:\wsdl\BenefitDeterminationProcess_BenefitDialogueServiceSOAP.wsdl', array('trace' => 1));
$procID = $client->start(array("prefix"=>"Genesis"));
$respXML = $client->__getLastResponse();
$requXML = $client->__getLastRequest();
echo "<p/>";
var_dump($procID);
//echo "<p/>";
var_dump($respXML);
//echo "<p/>";
var_dump($requXML);
$exchange = $client->exchangeOptions(array("processId"=>$procID)); //LINE 15
$end = $client->stop(array("processId"=>$procID));
?>

回答by Chris Baker

Whatever the $client->start()method is returning, it is typed as an object. You can access the properties of the object using the ->operator:

无论$client->start()方法返回什么,它都被输入为一个对象。您可以使用->运算符访问对象的属性:

$procID = $client->start(array("prefix"=>"Genesis"));

...

$exchange = $client->exchangeOptions(array("processId"=>$procID->processId));

This was probably an array, but is getting typed into an object. Thus, you end up with the stdClass.

这可能是一个数组,但正在输入到一个对象中。因此,您最终会得到stdClass

Another (and possibly better) way to do this is to type the return. That way, you don't have to make a new array for later passing as argument:

另一种(可能更好)的方法是输入返回值。这样,您就不必为以后作为参数传递而创建新数组:

$procID = (array) $client->start(array("prefix"=>"Genesis"));

...

$exchange = $client->exchangeOptions($procID);
$end = $client->stop($procID);