Ruchika
Ruchika

Reputation: 515

PHP Object of class stdClass could not be converted to string

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 -

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?

Thanks

Upvotes: 3

Views: 77053

Answers (4)

bwoebi
bwoebi

Reputation: 23777

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

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

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

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

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

Upvotes: 4

user3728517
user3728517

Reputation: 157

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:)

Upvotes: 3

Andrew Koper
Andrew Koper

Reputation: 7191

With primitive variable and strings, $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";

Upvotes: 0

Ben
Ben

Reputation: 57227

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

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

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:

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

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


Also, when you do

$a = echo ...

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

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

Upvotes: 3

Related Questions