Connecting Tech Pros Worldwide Forums | Help | Site Map

Classes Can't Contain Object Members?

Derek Battams
Guest
 
Posts: n/a
#1: Jul 17 '05
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
Guest
 
Posts: n/a
#2: Jul 17 '05

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]

sam
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Classes Can't Contain Object Members?


"Derek Battams" <derek@stopspam.battams.ca> wrote in message
news:139160af5571c4478ec2a2999bd10f85@news.teranew s.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]


Closed Thread