By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,745 Members | 1,867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,745 IT Pros & Developers. It's quick & easy.

Classes Can't Contain Object Members?

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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"
<de***@stopspam.battams.ca> wrote:
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


Jul 17 '05 #2

P: n/a
sam
"Derek Battams" <de***@stopspam.battams.ca> wrote in message
news:13******************************@news.teranew s.com...
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();
It works fine for me with the reference operator:
$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
No error if you do as I said before.
return;
}
}

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

Help appreciated,

Derek

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.