Classes Can't Contain Object Members?

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

    Classes Can't Contain Object Members?

    Using PHP 4.3.3; I'm trying to write a class that has as a member an
    instance of another class. Assume class B is already written and supports
    the method "method()".

    class A {
    var _desc;
    var _obj;

    function A() {
    $this->_desc = "Desc test";
    $this->_obj = new B();
    $this->_obj->method(); // Works here
    }

    function helper() {
    $this->_obj->method(); // Parse error claiming that I'm calling a
    member function on a non-object
    return;
    }
    }

    Is this not possible in PHP? Is there another way?

    Help appreciated,

    Derek
  • warstar

    #2
    Re: Classes Can't Contain Object Members?

    Yeah it is possible @ least i use it this wayi can't see the problem
    maybe this will work:
    class A {
    var _desc;
    var _obj;

    function A() {
    $this->_desc = "Desc test";

    $this->_obj->method(); // Works here
    }

    function new obj()
    {
    $this->_obj = new B();
    }

    function helper() {
    $this->_obj->method();
    }
    }

    i dunno maybe try a older version of php i my self use 4.3.2 and so
    think simular works there

    On Sat, 11 Oct 2003 20:42:16 GMT, "Derek Battams"
    <derek@stopspam .battams.ca> wrote:
    [color=blue]
    >Using PHP 4.3.3; I'm trying to write a class that has as a member an
    >instance of another class. Assume class B is already written and supports
    >the method "method()".
    >
    >class A {
    > var _desc;
    > var _obj;
    >
    > function A() {
    > $this->_desc = "Desc test";
    > $this->_obj = new B();
    > $this->_obj->method(); // Works here
    > }
    >
    > function helper() {
    > $this->_obj->method(); // Parse error claiming that I'm calling a
    >member function on a non-object
    > return;
    > }
    >}
    >
    >Is this not possible in PHP? Is there another way?
    >
    >Help appreciated,
    >
    >Derek[/color]

    Comment

    • sam

      #3
      Re: Classes Can't Contain Object Members?

      "Derek Battams" <derek@stopspam .battams.ca> wrote in message
      news:139160af55 71c4478ec2a2999 bd10f85@news.te ranews.com...[color=blue]
      > Using PHP 4.3.3; I'm trying to write a class that has as a member an
      > instance of another class. Assume class B is already written and supports
      > the method "method()".
      >
      > class A {
      > var _desc;
      > var _obj;
      >
      > function A() {
      > $this->_desc = "Desc test";
      > $this->_obj = new B();[/color]

      It works fine for me with the reference operator:
      $this->_obj = &new B();
      [color=blue]
      > $this->_obj->method(); // Works here
      > }
      >
      > function helper() {
      > $this->_obj->method(); // Parse error claiming that I'm calling a
      > member function on a non-object[/color]

      No error if you do as I said before.
      [color=blue]
      > return;
      > }
      > }
      >
      > Is this not possible in PHP? Is there another way?
      >
      > Help appreciated,
      >
      > Derek[/color]


      Comment

      Working...