473,410 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Object composition in PHP

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
5 3304
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
..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
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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I...
4
by: Patrick | last post by:
I want to achieve the following behaviour but I am having trouble getting there...bare with me im knew to c++ , so its probably rather trivial! To have a class ClassA, and composed within this...
4
by: fog | last post by:
Given: class A; and B "has-a" A. The composition relationship between A and B can be implemented in three ways: =================== # 1. class B {
1
by: Tony Johansson | last post by:
Hello experts! I know it's easy to override when you have inheritance. But if you instead use object composition having a pointer to a class X. If you want to override when having this object...
4
by: cmrchs | last post by:
Hi, how do I implement aggregation and how composition in C# ? When I say : an Airplane has a Pilot then I use aggregation but when I say : an Airplane has a Cockpit then I use composition. How...
4
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
2
by: Gary Wessle | last post by:
Hi what is the difference "in code writing" between aggregation and composition? is the following correct? aggregation (life time of both objects are independent). class A{ /* ... */ };...
4
by: Kay Schluehr | last post by:
As you know, there is no operator for function composition in Python. When you have two functions F and G and want to express the composition F o G you have to create a new closure lambda...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.