473,545 Members | 289 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 $controllerForA ll
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 $controllerForA ll;
function McShow() {
$this->controllerForA ll = $GLOBALS["controllerForA ll"];
}
Jul 17 '05 #1
6 2456
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->controllerForA ll = $GLOBALS["controllerForA ll"];
}
In my opinion it would be better to use a reference instead
of a copy:

$this->controllerForA ll =& $GLOBALS["controllerForA ll"];
If I have a class called $controllerForA ll 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 "controllerForA ll", don't you?

I have used such constructions and It worked fine. But
as mentioned before, you should use references, otherwise
the "controllerForA ll" 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 $controllerForA ll
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*******@airpo st.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 $controllerForA ll 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 "controllerForA ll", don't you?

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


Yes, sorry, I meant an instance of the class "McControllerFo rAll". As
soon as a page loads, an instance called $controllerForA ll is made. It
exists in global space. You are right, I should use references.
Jul 17 '05 #4
luc wastiaux <du*******@airp ost.net> wrote in message news:<bm******* **@enews4.newsg uy.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 $controllerForA ll
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 = & $controllerForA ll->getObject("McU sers");
$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->controllerForA ll = & $GLOBALS["controllerForA ll"];
$this->sql = $controllerForA ll->getObject("McS ql");
}

So now it McUsers is contained inside of an array in
$controllerForU sers, and also has a reference to $controllerForU sers
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 $controllerForA ll that is used. But the second
time McUsers is needed, the code again calls getObject() from the
$controllerForA ll 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($name OfClassToBeUsed ) {
if (!$nameOfClassT oBeUsed) {
print "Sorry, but the code wants to create a software object, and
yet in the method called 'getObject' and in the class called
McControllerFor All, 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 'defaultQueryOb ject' is
set incorrectly.";
return false;
}
if (array_key_exis ts($nameOfClass ToBeUsed,
$this->arrayOfAllTheO bjectsSoFarLoad ed)) {
$object = & $this->arrayOfAllTheO bjectsSoFarLoad ed[$nameOfClassToB eUsed];
return $object;
} elseif (class_exists($ nameOfClassToBe Used)) {
// 10-13-03 - in the 2 lines above we look to see if an instance of
this class already exists in
// $this->arrayOfAllTheO bjectsSoFarLoad ed. 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 $nameOfClassToB eUsed();
$this->arrayOfAllTheO bjectsSoFarLoad ed[$nameOfClassToB eUsed] =
$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($nameOf ClassToBeUsed);
// 10-13-03 - now that we've run import() we try again to see if
this class exists
if (class_exists($ nameOfClassToBe Used)) {
$object = new $nameOfClassToB eUsed();
$this->arrayOfAllTheO bjectsSoFarLoad ed[$nameOfClassToB eUsed] =
$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 $nameOfClassToB eUsed and the object known as controllerForAl l
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 $controllerForA ll
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 $controllerForA ll;
function McShow() {
$this->controllerForA ll = $GLOBALS["controllerForA ll"];
}


--
BOFH Excuse #130:

new management

Jul 17 '05 #6
Dirk Engels <d.******@stude nt.utwente.nl> wrote in message news:<bm******* ***@netlx020.ci v.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 $controllerForA ll
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 $controllerForA ll;
function McShow() {
$this->controllerForA ll = $GLOBALS["controllerForA ll"];
}

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1261
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 my data structure : A project would be made of tasks, and each task can contain sub-tasks, so I would have something like
4
3121
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 hierarchy is organized by Folder objects. A Folder object can contain either Apples, or Oranges, or other Folders. A Folder contains only one type of...
8
1841
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 database and I can’t seem to understand how I would name my objects. For example, I have an employee’s database with all the info I want an employee to...
3
4202
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 that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
12
7482
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 it to reset info in a combo box. Below is some sample code for my view interface and the presenter: public interface IDevToolView
0
2023
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. Ok my problem is the following. I have a class that contains a "MakeByteArray" function. I have many objects of that class. Inside that...
32
2515
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 why is it different from = ?
16
1645
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 worked for strings. Thanks in advance, Damien
275
12074
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
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
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 we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.