I'm very new to PHP and to programming in general and have a simple
question about oject composition in PHP. Here's a couple of simple
classes that I have created. The Greeting class 'has-a' Message object
as one of its members. The Message class seems to work fine, and the
Greeting class lets me create a Greeting object fine. However, I get
an error when trying to access the getMessage() function of the
Message class from within the Greeting class. All the code is below.
Anyone have any idea what the problem is?
class Message
{
protected $message;
public function __construct($me ssageArg)
{
$this->message = $messageArg;
}
public function getMessage()
{
return $this->message;
}
public function printMessage()
{
echo "<h1>$this->message</h1>";
}
} // end of Message class
class Greeting
{
private $to;
private $from;
private $mess; // a Message type
public function __construct($to Arg, $fromArg, $m)
{
$this->to = $toArg;
$this->from = $fromArg;
$this->mess = new Message($m);
}
public function printGreeting()
{
echo "
From: $this->from<br />
To: $this->to<br />
Message: $this->mess->getMessage <br />
";
}
} // end of Greeting class
-------------------------------------------------------------------------------------------------
Here's the PHP code that tries out these classes:
$m = new Message("Hello, World!");
$m->printMessage() ; // prints fine
echo "<hr />";
$g = new Greeting("Rober t","Sally", $m); // no errors
$g->printGreeting( ); // generates error, below
Catchable fatal error: Object of class Message could not be converted
to string in ...
question about oject composition in PHP. Here's a couple of simple
classes that I have created. The Greeting class 'has-a' Message object
as one of its members. The Message class seems to work fine, and the
Greeting class lets me create a Greeting object fine. However, I get
an error when trying to access the getMessage() function of the
Message class from within the Greeting class. All the code is below.
Anyone have any idea what the problem is?
class Message
{
protected $message;
public function __construct($me ssageArg)
{
$this->message = $messageArg;
}
public function getMessage()
{
return $this->message;
}
public function printMessage()
{
echo "<h1>$this->message</h1>";
}
} // end of Message class
class Greeting
{
private $to;
private $from;
private $mess; // a Message type
public function __construct($to Arg, $fromArg, $m)
{
$this->to = $toArg;
$this->from = $fromArg;
$this->mess = new Message($m);
}
public function printGreeting()
{
echo "
From: $this->from<br />
To: $this->to<br />
Message: $this->mess->getMessage <br />
";
}
} // end of Greeting class
-------------------------------------------------------------------------------------------------
Here's the PHP code that tries out these classes:
$m = new Message("Hello, World!");
$m->printMessage() ; // prints fine
echo "<hr />";
$g = new Greeting("Rober t","Sally", $m); // no errors
$g->printGreeting( ); // generates error, below
Catchable fatal error: Object of class Message could not be converted
to string in ...
Comment