3 Replies - 128117 Views - Last Post: 12 May 2009 - 05:11 PM Rate Topic: -----

#1 amnesia623   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-May 09

Object of class could not be converted to string...?

Posted 12 May 2009 - 04:41 PM

class message {
	
	public function show_message($message) {
		$output = $message;
		return $output;
	}
	
}

if (isset($_POST['submit'])) {
	$thismessage = new message;
	$thismessage->show_message("Inserted Successfully");
	echo $thismessage;
	exit();
	}
 


error:

Quote

Catchable fatal error: Object of class message could not be converted to string in /Users/Sites... on line 8


I have seen this a couple other times when I'm trying to echo data from the database.
PHP version 5.25

Eventually I am going to add more to this class so I can post user messages and such.


What am I doing wrong?
Thanks

This post has been edited by amnesia623: 12 May 2009 - 04:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Object of class could not be converted to string...?

#2 ShaneK   User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Object of class could not be converted to string...?

Posted 12 May 2009 - 05:05 PM

It's because you're trying to print out the object as a string. (I believe, actually I'm not sure because I pretty much just got started with OOP)

But here, this works:
print $thismessage->show_message("Inserted Successfully");


Just change that line to look like that and remove the echo and you'll be set.

When you did that, $thismessage was still an object - it had just ran a function. If you wanted to return the message as a variable you should do something like this:

class message {
	var $message;
	public function show_message($message) {
		$output = $message;
		$this->message = $output;
	}
	
}

if (isset($_POST['submit'])) {
	$thismessage = new message;
	$thismessage->show_message("Inserted Successfully");
	echo($thismessage->message);
	exit();
}


Yours,
Shane~

This post has been edited by ShaneK: 12 May 2009 - 05:12 PM

Was This Post Helpful? 1
  • +
  • -

#3 amnesia623   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-May 09

Re: Object of class could not be converted to string...?

Posted 12 May 2009 - 05:10 PM

*smack head* ohhh, that makes sense!

Thank you
Was This Post Helpful? 0
  • +
  • -

#4 ShaneK   User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Object of class could not be converted to string...?

Posted 12 May 2009 - 05:11 PM

Note that I changed my message after you replied, just wanted to point that out here.

That'll show you how to return it as a variable.

No problem ^_^

Yours,
Shane~

This post has been edited by ShaneK: 12 May 2009 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1