• PHP Help PHP Coding
  • Catchable fatal error: Object of class Category could not be converted to string

Hi everyone,

I get this error:

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

The code:

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

    Just like the error message says, you're trying to echo an object as if it were a string.

      bradgrafelman;10970770 wrote:

      Just like the error message says, you're trying to echo an object as if it were a string.

      Is there a way i can convert the object to a string or is there some other way to do it?

        laanes wrote:

        Is there a way i can convert the object to a string

        No, hence the error message.

        laanes wrote:

        is there some other way to do it?

        Yes - use the object as it was intended.

        Based on your questions, you should probably find some basic OOP tutorials to understand how OOP works, and then find the documentation for the class you're using to see what type of object the Category::find_all() method returns.

          Based on your questions, you should probably find some basic OOP tutorials to understand how OOP works, and then find the documentation for the class you're using to see what type of object the Category::find_all() method returns.

          Thank you for your feedback,

          I will try to find out how to display the categories the right way.

          laanes

            Write a Reply...