can object contain object that contains it? 
July 17th, 2005, 12:31 AM
| | | can object contain object that contains it?
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"];
} | 
July 17th, 2005, 12:32 AM
| | | Re: can object contain object that contains it?
Hi Lawrence,
[color=blue]
> How dangerous or stupid is it for an object to have
> a reference to the object which contains it?[/color]
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.
[color=blue]
> function McShow() {
> $this->controllerForAll = $GLOBALS["controllerForAll"];
> }[/color]
In my opinion it would be better to use a reference instead
of a copy:
$this->controllerForAll =& $GLOBALS["controllerForAll"];
[color=blue]
> 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?[/color]
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 | 
July 17th, 2005, 12:32 AM
| | | Re: can object contain object that contains it?
lawrence wrote:
[color=blue]
> 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?[/color]
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: dustpuppy@airpost.net
jabber: luc@jabber.4002.org
ICQ: 76235870 | 
July 17th, 2005, 12:32 AM
| | | Re: can object contain object that contains it?
"Fabian Wleklinski" <Wleklinski.NNTP@eWorks.de> wrote in message[color=blue][color=green]
> > 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?[/color]
>
> 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.[/color]
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. | 
July 17th, 2005, 12:32 AM
| | | Re: can object contain object that contains it?
luc wastiaux <dustpuppy@airpost.net> wrote in message news:<bmgdv8118d0@enews4.newsguy.com>...[color=blue]
> lawrence wrote:
>[color=green]
> > 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?[/color]
>
> 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 ?[/color]
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>";
}
}
} | 
July 17th, 2005, 12:38 AM
| | | Re: can object contain object that contains it?
Sounds like a GOD class...
Dirk
lawrence wrote:
[color=blue]
> 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"];
> }[/color]
--
BOFH Excuse #130:
new management | 
July 17th, 2005, 12:43 AM
| | | Re: can object contain object that contains it?
Dirk Engels <d.engels@student.utwente.nl> wrote in message news:<bmsuq4$k5v$1@netlx020.civ.utwente.nl>...[color=blue]
> Sounds like a GOD class...[/color]
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.
[color=blue]
>
> Dirk
>
> lawrence wrote:
>[color=green]
> > 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"];
> > }[/color][/color] | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|