By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

Object composition in PHP

P: n/a
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($messageArg)
{
$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($toArg, $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("Robert","Sally", $m); // no errors
$g->printGreeting(); // generates error, below

Catchable fatal error: Object of class Message could not be converted
to string in ...
Sep 18 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Sep 18, 7:42*pm, robean <st1...@gmail.comwrote:
*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($messageArg)
* * * * {
* * * * * * * * $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($toArg, $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("Robert","Sally", $m); // no errors
$g->printGreeting(); // generates error, below

Catchable fatal error: Object of class Message could not be converted
to string in ...
getMessage is a method not a property so

Message:". $this->mess->getMessage() ."<br />

and to avoid later problems:

echo "
From: {$this->from}<br />
To: {$this->to}<br />
Message:". $this->mess->getMessage ." <br />
";
Sep 18 '08 #2

P: n/a
..oO(Captain Paralytic)
>On Sep 18, 7:42*pm, robean <st1...@gmail.comwrote:
>>
Catchable fatal error: Object of class Message could not be converted
to string in ...

getMessage is a method not a property so

Message:". $this->mess->getMessage() ."<br />

and to avoid later problems:

echo "
From: {$this->from}<br />
To: {$this->to}<br />
Message:". $this->mess->getMessage ." <br />
";
Better:

printf("From: %s<br />\nTo: %s<br />\nMessage: %s<br />\n",
$this->from,
$this->to,
$this->mess->getMessage()
);

Micha
Sep 18 '08 #3

P: n/a
On 18 Sep, 20:17, Michael Fesser <neti...@gmx.dewrote:
.oO(Captain Paralytic)
On Sep 18, 7:42*pm, robean <st1...@gmail.comwrote:
Catchable fatal error: Object of class Message could not be converted
to string in ...
getMessage is a method not a property so
Message:". $this->mess->getMessage() ."<br />
and to avoid later problems:
* * * * * * * * * *echo "
* * * * * * * * * * * *From: {$this->from}<br />
* * * * * * * * * * * *To: {$this->to}<br />
* * * * * * * * * * * *Message:". $this->mess->getMessage ." <br />
* * * * * * * * * * * *";

Better:

printf("From: %s<br />\nTo: %s<br />\nMessage: %s<br />\n",
* $this->from,
* $this->to,
* $this->mess->getMessage()
);

Micha
Different I grant, but why necessarily better?
Sep 18 '08 #4

P: n/a
..oO(Captain Paralytic)
>On 18 Sep, 20:17, Michael Fesser <neti...@gmx.dewrote:
>.oO(Captain Paralytic)
>On Sep 18, 7:42*pm, robean <st1...@gmail.comwrote:
>Catchable fatal error: Object of class Message could not be converted
to string in ...
>getMessage is a method not a property so
>Message:". $this->mess->getMessage() ."<br />
>and to avoid later problems:
* * * * * * * * * *echo "
* * * * * * * * * * * *From: {$this->from}<br />
* * * * * * * * * * * *To: {$this->to}<br />
* * * * * * * * * * * *Message:". $this->mess->getMessage ." <br />
* * * * * * * * * * * *";

Better:

printf("From: %s<br />\nTo: %s<br />\nMessage: %s<br />\n",
* $this->from,
* $this->to,
* $this->mess->getMessage()
);

Micha

Different I grant, but why necessarily better?
Easier to maintain and much more flexible. You don't have to watch for
correct quoting, escaping, curly braces and all that stuff. I consider
echo statements like above with a lot of embedded variables way too
error-prone and just ugly. Of course the latter is just personal taste.

Micha
Sep 18 '08 #5

P: n/a
On Sep 18, 12:24 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Captain Paralytic)
On 18 Sep, 20:17, Michael Fesser <neti...@gmx.dewrote:
.oO(Captain Paralytic)
On Sep 18, 7:42 pm, robean <st1...@gmail.comwrote:
Catchable fatal error: Object of class Message could not be converted
to string in ...
getMessage is a method not a property so
Message:". $this->mess->getMessage() ."<br />
and to avoid later problems:
echo "
From: {$this->from}<br />
To: {$this->to}<br />
Message:". $this->mess->getMessage ." <br />
";
Better:
printf("From: %s<br />\nTo: %s<br />\nMessage: %s<br />\n",
$this->from,
$this->to,
$this->mess->getMessage()
);
Micha
Different I grant, but why necessarily better?

Easier to maintain and much more flexible. You don't have to watch for
correct quoting, escaping, curly braces and all that stuff. I consider
echo statements like above with a lot of embedded variables way too
error-prone and just ugly. Of course the latter is just personal taste.

Micha
Many thanks to both of you. The code works fine now.
Sep 18 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.