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>";
}
}
}