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

Using OO in PHP5

P: n/a
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->methodInOtherObject("something");
}
}

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.

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
no****@yakhair.com wrote:
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();
}

class cMyObject {
private $foo;

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


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

Jul 17 '05 #2

P: n/a
.oO(no****@yakhair.com)
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?
Sure.
I'd like to do something like this...

class cMyObject {
public function __construct() {
private $foo = new cOtherObject();
}
The variable $foo will only be valid in the context of the constructor.
To access instance variables use the $this->varName syntax.
This is a very simplistic example that doesn't really relate why I want
to do this, but is it possible?
Sure, but you should declare variables before using them (at least when
using classes):

class cMyObject {
private $foo;

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

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


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
Jul 17 '05 #3

P: n/a
<no****@yakhair.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
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->methodInOtherObject("something");
}
}

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


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($params) {
$this->foo = new cOtherObject($params);
}
}

Jul 17 '05 #4

P: n/a
CJ Llewellyn wrote:
class cMyObject {
private $foo = new cOtherObject();
}


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

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.