473,500 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"];
}
Jul 17 '05 #1
6 2450
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1254
by: Olivier Jullian | last post by:
Hi, I'm new to .NET and am trying to take advantage of the object structure while accessing relational databases. I started a small project for managing "projects". Here is a description of...
4
3097
by: Dave Veeneman | last post by:
I'm puzzling over the best design for a Folder object. I have two basic domain objects; leat's call them an Apple and an Orange. The objects are maintained in separate hierarchies, and each...
8
1837
by: Lance | last post by:
I've been teaching myself C# for a few months now and I have a concept problem. I know how to instantiate an object from a class I’ve created, but now I want to retrieve and store data in a...
3
4198
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
12
7475
by: Doug | last post by:
Hi, I learned a little about the model view presenter pattern at a conference this last week and am experimenting with it. It's working pretty well but I have a question. I am trying to use...
0
2017
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
32
2494
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and...
16
1634
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
275
12031
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7156
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7030
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7409
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5511
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3117
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3115
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
688
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.