Object of class Category could not be converted to string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
laanes
Forum Newbie
Posts: 7
Joined: Thu Jan 28, 2010 4:38 am

Object of class Category could not be converted to string

Post by laanes »

Hi everyone,

I get this error:

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

The code:

Code: Select all

public static function list_all_cat_names() {
  	
  		$result_array = self::find_by_sql("SELECT name FROM ".self::$table_name);
  		return !empty($result_array) ? array_shift($result_array) : false;
  	
}


$categories = Category::find_all();

foreach ($categories as $cats):

echo $cats;

endforeach;
Where could be the problem?

Kind regards,
laanes
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Object of class Category could not be converted to strin

Post by Jonah Bron »

You're trying to echo an object. Either that's not what you want to do, or you need to implement the __toString() magic method.

http://php.net/language.oop5.magic#lang ... c.tostring
Post Reply