adamcannon Posted April 10, 2007 Share Posted April 10, 2007 I receive the error "Catchable fatal error: Object of class Car could not be converted to string" whenever I try to echo one of its attributes. For example: $car = new car(); echo $car->model; model is a public attribute so is this error normal? I'm trying to avoid writing a display function or passing the attribute's value to a local variable. Thanks for reading. Link to comment https://forums.phpfreaks.com/topic/46430-can-you-echo-object-attributes/ Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 What is the class code? Link to comment https://forums.phpfreaks.com/topic/46430-can-you-echo-object-attributes/#findComment-225837 Share on other sites More sharing options...
adamcannon Posted April 10, 2007 Author Share Posted April 10, 2007 Hi frost. I don't think it matters. The class could be: class Car { public $model = "ford"; } Link to comment https://forums.phpfreaks.com/topic/46430-can-you-echo-object-attributes/#findComment-225849 Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 Could be huh, where is the constructor. Are you using PHP4 or PHP5 ? In order for a class to be initiated a constructor must be set, do not hold me to that but that is the general rule of thumb. class Car { var $model = ""; function Car() { $this->model = 'Ford'; } } The above will work with PHP 4. Link to comment https://forums.phpfreaks.com/topic/46430-can-you-echo-object-attributes/#findComment-225857 Share on other sites More sharing options...
adamcannon Posted April 10, 2007 Author Share Posted April 10, 2007 I'm using PHP 5. You can create classes without a constructor. Anyhoo, I found the problem. It appears you CAN use echo as long as the object's attribute isn't in quotations. Link to comment https://forums.phpfreaks.com/topic/46430-can-you-echo-object-attributes/#findComment-225891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.