Jump to content

Can you ECHO object attributes?


adamcannon

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.