Object composition in PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robean

    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($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 ...
  • Captain Paralytic

    #2
    Re: Object composition in PHP

    On Sep 18, 7:42 pm, robean <st1...@gmail.c omwrote:
     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 ...
    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 />
    ";

    Comment

    • Michael Fesser

      #3
      Re: Object composition in PHP

      ..oO(Captain Paralytic)
      >On Sep 18, 7:42 pm, robean <st1...@gmail.c omwrote:
      >>
      >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

      Comment

      • Captain Paralytic

        #4
        Re: Object composition in PHP

        On 18 Sep, 20:17, Michael Fesser <neti...@gmx.de wrote:
        .oO(Captain Paralytic)
        >
        >
        >
        On Sep 18, 7:42 pm, robean <st1...@gmail.c omwrote:
        >
        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?

        Comment

        • Michael Fesser

          #5
          Re: Object composition in PHP

          ..oO(Captain Paralytic)
          >On 18 Sep, 20:17, Michael Fesser <neti...@gmx.de wrote:
          >.oO(Captain Paralytic)
          >>
          >>
          >>
          >On Sep 18, 7:42 pm, robean <st1...@gmail.c omwrote:
          >>
          >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

          Comment

          • robean

            #6
            Re: Object composition in PHP

            On Sep 18, 12:24 pm, Michael Fesser <neti...@gmx.de wrote:
            .oO(Captain Paralytic)
            >
            >
            >
            On 18 Sep, 20:17, Michael Fesser <neti...@gmx.de wrote:
            .oO(Captain Paralytic)
            >
            On Sep 18, 7:42 pm, robean <st1...@gmail.c omwrote:
            >
            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.

            Comment

            Working...