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

can object contain object that contains it?

P: n/a
How dangerous or stupid is it for an object to have a reference to the
object which contains it? If I have a class called $controllerForAll
which has an arrray of all the objects that exist, what happens if one
of those objects, when it is created, takes a reference to the object
that contains it? Do bad things happen?


class McShow {

var $dsArray;
var $htmlObject;
var $loopObject;
var $getInfoObject;
var $controllerForAll;
function McShow() {
$this->controllerForAll = $GLOBALS["controllerForAll"];
}
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Lawrence,
How dangerous or stupid is it for an object to have
a reference to the object which contains it?
As far as I know PHP does not have any problems with such
constructions. But in some cases it is not a good design
to let instances know informations about their containing
instances.
function McShow() {
$this->controllerForAll = $GLOBALS["controllerForAll"];
}
In my opinion it would be better to use a reference instead
of a copy:

$this->controllerForAll =& $GLOBALS["controllerForAll"];
If I have a class called $controllerForAll which has an
arrray of all the objects that exist, what happens if one
of those objects, when it is created, takes a reference
to the object that contains it? Do bad things happen?


You mean an instance called "controllerForAll", don't you?

I have used such constructions and It worked fine. But
as mentioned before, you should use references, otherwise
the "controllerForAll" may not be up to date at some time.

Greetings from Frankfurt/Germany,

Fabian Wleklinski
Jul 17 '05 #2

P: n/a
lawrence wrote:
How dangerous or stupid is it for an object to have a reference to the
object which contains it? If I have a class called $controllerForAll
which has an arrray of all the objects that exist, what happens if one
of those objects, when it is created, takes a reference to the object
that contains it? Do bad things happen?


Nothing bad will happen, but there may be a more OO way of doing it. Why
do you need to have access to the container class ?

--
luc wastiaux - email: du*******@airpost.net
jabber: lu*@jabber.4002.org
ICQ: 76235870

Jul 17 '05 #3

P: n/a
"Fabian Wleklinski" <Wl*************@eWorks.de> wrote in message
If I have a class called $controllerForAll which has an
arrray of all the objects that exist, what happens if one
of those objects, when it is created, takes a reference
to the object that contains it? Do bad things happen?


You mean an instance called "controllerForAll", don't you?

I have used such constructions and It worked fine. But
as mentioned before, you should use references, otherwise
the "controllerForAll" may not be up to date at some time.


Yes, sorry, I meant an instance of the class "McControllerForAll". As
soon as a page loads, an instance called $controllerForAll is made. It
exists in global space. You are right, I should use references.
Jul 17 '05 #4

P: n/a
luc wastiaux <du*******@airpost.net> wrote in message news:<bm*********@enews4.newsguy.com>...
lawrence wrote:
How dangerous or stupid is it for an object to have a reference to the
object which contains it? If I have a class called $controllerForAll
which has an arrray of all the objects that exist, what happens if one
of those objects, when it is created, takes a reference to the object
that contains it? Do bad things happen?


Nothing bad will happen, but there may be a more OO way of doing it. Why
do you need to have access to the container class ?


I want an array which stores every object that gets created. When an
object is needed in the client code, rather than create a new copy of
the object, I want to first see if it has already been created. The
array, and the method for getting it, must exist in global space. You
can see the method getObject() below.

Now if I have a class called McUsers, and you try to login to the
control panel of my website, the client code will create an instance
of McUsers, call it $users, and use this class to see if you have the
security rating to login. So it might look like this:

$users = & $controllerForAll->getObject("McUsers");
$users->checkPassword();

But $users, to work, must have a way of reaching the database. So in
its constructor, it does something like this:

function McUsers() {
$this->controllerForAll = & $GLOBALS["controllerForAll"];
$this->sql = $controllerForAll->getObject("McSql");
}

So now it McUsers is contained inside of an array in
$controllerForUsers, and also has a reference to $controllerForUsers
inside of itself, so that it can get other objects using the
getObject() method. The first time McUsers is called, it is not the
copy in the array in $controllerForAll that is used. But the second
time McUsers is needed, the code again calls getObject() from the
$controllerForAll object which exists in global space, and now this
object is going to return a reference to the object $users which is
stored in the array.

You may have suggestions about how this might better be organized.
Below you can see the method getObject();

function & getObject($nameOfClassToBeUsed) {
if (!$nameOfClassToBeUsed) {
print "Sorry, but the code wants to create a software object, and
yet in the method called 'getObject' and in the class called
McControllerForAll, it's just being handed an empty string instead of
the correct name for whatever software object it is supposed to
create. The most common reason for this to happen is if something is
set wrong in the config file, especially if 'defaultQueryObject' is
set incorrectly.";
return false;
}
if (array_key_exists($nameOfClassToBeUsed,
$this->arrayOfAllTheObjectsSoFarLoaded)) {
$object = & $this->arrayOfAllTheObjectsSoFarLoaded[$nameOfClassToBeUsed];
return $object;
} elseif (class_exists($nameOfClassToBeUsed)) {
// 10-13-03 - in the 2 lines above we look to see if an instance of
this class already exists in
// $this->arrayOfAllTheObjectsSoFarLoaded. If so, we want to return
it. But if not, then we want to see
// whether such a class is known of. If yes, then we create an
instance of it. If not, then we will skip down
// below and run import() in the hopes of finding it.
$object = new $nameOfClassToBeUsed();
$this->arrayOfAllTheObjectsSoFarLoaded[$nameOfClassToBeUsed] =
$object;
return $object;
} else {
// 10-10-03 - hopefully this method doesn't get this far because
the needed object has already been found
// and the "return" keyword has stopped execution of the method.
But if we get this far, then we run import
// because we've got to find this object
$this->import($nameOfClassToBeUsed);
// 10-13-03 - now that we've run import() we try again to see if
this class exists
if (class_exists($nameOfClassToBeUsed)) {
$object = new $nameOfClassToBeUsed();
$this->arrayOfAllTheObjectsSoFarLoaded[$nameOfClassToBeUsed] =
$object;
return $object;
} else {
print "<hr>Error: Awful sorry, but after poking about a bit the
software still can't quite to seem to find a file, object, or class
called $nameOfClassToBeUsed and the object known as controllerForAll
really needs it. It feels badly about this, of course, and worries
this will have a negative effect on your day. However, the real shod
in this case is probably not the software itself, but some programmer,
who was probably quite gone when they wrote whatever lines of code
necessitated the software printing this error. However, in fairness,
we must ask if you haven't moved any files lately, or played wreck
with your installation, or poked about where you shouldn't have, and
if yes, could you please put everything back the way it was? Please
note, if you're a programmer, and you're debugging, and the software
can't find a PHP file which you know is there, that means there is a
parse error in that file.<hr>";
}
}
}
Jul 17 '05 #5

P: n/a
Sounds like a GOD class...

Dirk

lawrence wrote:
How dangerous or stupid is it for an object to have a reference to the
object which contains it? If I have a class called $controllerForAll
which has an arrray of all the objects that exist, what happens if one
of those objects, when it is created, takes a reference to the object
that contains it? Do bad things happen?


class McShow {

var $dsArray;
var $htmlObject;
var $loopObject;
var $getInfoObject;
var $controllerForAll;
function McShow() {
$this->controllerForAll = $GLOBALS["controllerForAll"];
}


--
BOFH Excuse #130:

new management

Jul 17 '05 #6

P: n/a
Dirk Engels <d.******@student.utwente.nl> wrote in message news:<bm**********@netlx020.civ.utwente.nl>...
Sounds like a GOD class...
That was partly my concern as well. I mean it to be a controller
class, not a GOD class. It has only 3 methods. Mostly it just fetches
files and objects and keeps instances of objects in an array, for
quick retrieval. The utility code is in those other objects that it
loads.




Dirk

lawrence wrote:
How dangerous or stupid is it for an object to have a reference to the
object which contains it? If I have a class called $controllerForAll
which has an arrray of all the objects that exist, what happens if one
of those objects, when it is created, takes a reference to the object
that contains it? Do bad things happen?


class McShow {

var $dsArray;
var $htmlObject;
var $loopObject;
var $getInfoObject;
var $controllerForAll;
function McShow() {
$this->controllerForAll = $GLOBALS["controllerForAll"];
}

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.