无法将类 stdClass 的 PHP 对象转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19342776/
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 19:19:26  来源:igfitidea点击:

PHP Object of class stdClass could not be converted to string

php

提问by Ruchika

I have made a small multiplication function

我做了一个小的乘法函数

 <p><?php
 $a= "<?php echo $this->prodDet->v_price?>";
 $b=.26;
 $c=$a*$b;
 echo $c;
 ?>

Here the price value is extracted from database of the requiste product and is multiple by fixed .26 variable. Wondering why am getting this errror -

这里的价格值是从需求产品的数据库中提取的,乘以固定的 0.26 变量。想知道为什么会出现此错误 -

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

I am learning the basics of PHP. can someone suggest how to get it solved please?

我正在学习 PHP 的基础知识。有人可以建议如何解决吗?

Thanks

谢谢

回答by bwoebi

You don't know what you're trying here?

你不知道你在这里尝试什么?

$a = "<?php echo $this->prodDet->v_price?>";

$alooks after the assignment like <?php echo <the value of $this->prodDet->v_price>?>. The <?phppart there is not executed. You surely want to write:

$a<?php echo <the value of $this->prodDet->v_price>?>. <?php那里的部分没有执行。你肯定想写:

$a = $this->prodDet->v_price;

And make sure that $ais not an object at the end! (what you can check via var_dump($a);in the line after the assignment)

并确保它$a最终不是一个对象!(您可以var_dump($a);在分配后的行中检查什么)

回答by Ben

v_pricehas been set as a class instance somewhere before these lines of code. My guess is that happens somewhere in prodDet.

v_price在这些代码行之前的某个地方已将其设置为类实例。我的猜测是发生在prodDet.

So, when you echoit, PHP tries to convert to a string and fails.

因此,当您使用echo它时,PHP 会尝试转换为字符串并失败。

To see what kind of class it is, try:

要查看它是哪种类,请尝试:

var_dump($this->prodDet->v_price);

That will help you debug.

这将帮助您调试。



An example is in PHP: Catchable fatal error: Object of class stdClass could not be converted to string, where the person accidentally sets a variable to be a class in this line:

一个例子是在PHP 中:Catchable fatal error: Object of class stdClass could not be convert to string,其中该人不小心将变量设置为这一行中的类:

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

then they try to echo $procIDand get the same error. You can't echo a class because it can't be changed to a string.

然后他们尝试回声$procID并得到相同的错误。您不能回显类,因为它不能更改为字符串。



Also, when you do

另外,当你做

$a = echo ...

$awill not be what you expect, since echojust prints out to the screen! It doesn't return the value. You'll want to remove the echoand just set it directly:

$a不会是你所期望的,因为echo只是打印到屏幕上!它不返回值。您将要删除echo并直接设置它:

$a = $this->prodDet->v_price->getSomeValue();

回答by user3728517

I also get same error message which is

我也收到相同的错误消息

enter image description here

在此处输入图片说明

after one hour checking everything i find that i have done some small mistake which is i have use variable sign ($) twice when fetching data. which is

一小时检查完所有内容后,我发现我犯了一些小错误,即我在获取数据时使用了变量符号 ($) 两次。这是

enter image description here

在此处输入图片说明

After remove one dollar sign it's working fine,it may be help someone:)

删除一个美元符号后它工作正常,它可能对某人有帮助:)

回答by Andrew Koper

With primitive variable and strings, $name = "Charlie";

使用原始变量和字符串, $name = "Charlie";

Objects store many, related things together. Strings, numbers, booleans, etc are stored as properties of the object. Properties are accessed with the object operator ("arrow operator") like $person->name = "Charlie";

对象将许多相关的东西存储在一起。字符串、数字、布尔值等存储为对象的属性。使用对象运算符(“箭头运算符”)访问属性,如 $person->name = "Charlie";