Using OO in PHP5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nospam@yakhair.com

    Using OO in PHP5

    I'm new to OO in PHP, so maybe this is a dumb question, but I was
    wondering if you can declare and use an object from within another
    object?

    I'd like to do something like this...

    class cMyObject {
    public function __construct() {
    private $foo = new cOtherObject();
    }

    public function checkWhatever($ input) {
    $this->foo->methodInOtherO bject("somethin g");
    }
    }

    This is a very simplistic example that doesn't really relate why I want
    to do this, but is it possible?

    If it is not possible, why does the following code not throw an error
    for an unexpected appearance of the "new" keyword? Bug?

    <?php
    $myTest = new cObj1;
    exit();

    class cObj1 {
    public function __construct() {
    $foo = new cObj2;
    }
    } // cObj1

    class cObj2 {
    private $thing;
    } // cObj2
    ?>

    My test environment is PHP 5.0.2.

  • Janwillem Borleffs

    #2
    Re: Using OO in PHP5

    nospam@yakhair. com wrote:[color=blue]
    > I'm new to OO in PHP, so maybe this is a dumb question, but I was
    > wondering if you can declare and use an object from within another
    > object?
    >
    > I'd like to do something like this...
    >
    > class cMyObject {
    > public function __construct() {
    > private $foo = new cOtherObject();
    > }
    >[/color]

    class cMyObject {
    private $foo;

    public function __construct() {
    $this->foo = new cOtherObject();
    }
    }
    [color=blue]
    > If it is not possible, why does the following code not throw an error
    > for an unexpected appearance of the "new" keyword? Bug?
    >[/color]

    It is, as long as you define the class properties the correct way.


    JW



    Comment

    • Michael Fesser

      #3
      Re: Using OO in PHP5

      .oO(nospam@yakh air.com)
      [color=blue]
      >I'm new to OO in PHP, so maybe this is a dumb question, but I was
      >wondering if you can declare and use an object from within another
      >object?[/color]

      Sure.
      [color=blue]
      >I'd like to do something like this...
      >
      >class cMyObject {
      >public function __construct() {
      >private $foo = new cOtherObject();
      >}[/color]

      The variable $foo will only be valid in the context of the constructor.
      To access instance variables use the $this->varName syntax.
      [color=blue]
      >This is a very simplistic example that doesn't really relate why I want
      >to do this, but is it possible?[/color]

      Sure, but you should declare variables before using them (at least when
      using classes):

      class cMyObject {
      private $foo;

      function __construct() {
      $this->foo = new cOtherObject();
      }

      ...
      }
      [color=blue]
      >If it is not possible, why does the following code not throw an error
      >for an unexpected appearance of the "new" keyword? Bug?[/color]

      No, the 'new' keyword is not unexpected in this case. If it would be,
      every assignment to a previously undeclared variable would cause an
      error. Except for the assigned value there's no difference between these
      statements:

      $foo = 42;
      $bar = new Something();

      Micha

      Comment

      • CJ Llewellyn

        #4
        Re: Using OO in PHP5

        <nospam@yakhair .com> wrote in message
        news:1105727772 .777264.56770@c 13g2000cwb.goog legroups.com...[color=blue]
        > I'm new to OO in PHP, so maybe this is a dumb question, but I was
        > wondering if you can declare and use an object from within another
        > object?
        >
        > I'd like to do something like this...
        >
        > class cMyObject {
        > public function __construct() {
        > private $foo = new cOtherObject();
        > }
        >
        > public function checkWhatever($ input) {
        > $this->foo->methodInOtherO bject("somethin g");
        > }
        > }
        >
        > This is a very simplistic example that doesn't really relate why I want
        > to do this, but is it possible?[/color]

        class cMyObject {
        private $foo = new cOtherObject();
        }

        This assumes that you don't pass any parameters to $foo otherwise

        class cMyObject {
        private $foo = null;

        function __construct($pa rams) {
        $this->foo = new cOtherObject($p arams);
        }
        }



        Comment

        • Janwillem Borleffs

          #5
          Re: Using OO in PHP5

          CJ Llewellyn wrote:[color=blue]
          > class cMyObject {
          > private $foo = new cOtherObject();
          > }
          >[/color]

          This throws a parse error because of the unexpected "new" keyword and the
          brackets. Only your second example is valid.


          JW



          Comment

          Working...