Connecting Tech Pros Worldwide Forums | Help | Site Map

object references stored in array gives me what?

lawrence
Guest
 
Posts: n/a
#1: Jul 17 '05
I'm nervous this method is not doing what I assumed it was doing. Does
it not create objects, store them in an array, and check that array to
see if a requested class has already been instantiated, in which case
it gets a reference out of the array and returns that, again by
reference?

I'm getting out of memory error, meaning my scrip is using the 8 megs
that PHP scripts are, by default, allowed. So I'm left to think I've
done something wrong with the references.





function & getObject($nameOfClassToBeUsed) {
if (!$nameOfClassToBeUsed) {
print "Sorry, but the code wants to create a software object, but
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>";
}
}
}

Fabian Wleklinski
Guest
 
Posts: n/a
#2: Jul 17 '05

re: object references stored in array gives me what?


Hi lawrence,

I don't think that this causes your problem, but there is
a reference-operator missing (two times), if you do not
want to clone the objects:
[color=blue]
> $this->arrayOfAllTheObjectsSoFarLoaded[
> $nameOfClassToBeUsed] = $object;[/color]

You should use =& instead of =. And are you assigning the
result of getObject() via &= ?

I can't see what should eat up the 8 megabyte of memory.
May be the import-function causes this problem? Or may be
there is a huge object being created?

Greetings from Frankfurt / Germany,

Fabian Wleklinski


lawrence
Guest
 
Posts: n/a
#3: Jul 17 '05

re: object references stored in array gives me what?


"Fabian Wleklinski" <Wleklinski.NNTP@eWorks.de> wrote in message news:<bmir68$1uq@library1.airnews.net>...[color=blue]
> Hi lawrence,
>
> I don't think that this causes your problem, but there is
> a reference-operator missing (two times), if you do not
> want to clone the objects:
>[color=green]
> > $this->arrayOfAllTheObjectsSoFarLoaded[
> > $nameOfClassToBeUsed] = $object;[/color]
>
> You should use =& instead of =. And are you assigning the
> result of getObject() via &= ?[/color]

You are right, I have made that change.




[color=blue]
> I can't see what should eat up the 8 megabyte of memory.
> May be the import-function causes this problem? Or may be
> there is a huge object being created?[/color]

It fails when it has to get out all the weblog entries for the page. I
think my front page posts 50 entries. On the advice of Phillip
Greenspun and Edward Tufte I allow my front page to be heavy with text
(I've been debugging on my personal site). But even then, it is only
about 200k, or 250k at most. I realize there are extraneous elements
that don't get printed. But even if they doubled the size, that would
only be 500k. Now some operations involve making a copy of this
array, like when I strip slashes from the array while copying it to a
new one. But even then, the size of the 2 arrays should only be a meg,
plus some overhead. It is hard for me to imagine where the extra 7
megs come from. I wish there was a way to take a snap shot of the
state of script at any given time.
Fabian Wleklinski
Guest
 
Posts: n/a
#4: Jul 17 '05

re: object references stored in array gives me what?


Hi Lawrence,

if you handle large objects (> 250K) it would make sense to free
some memory explicitely.

Example given, after an operation like $text=addslashes($oldtext)
you could free $oldtext using unset( $oldtext ), or alternatively
using the same variable:

$text = addslashes($text); // this works as well

Search for all assignments concerning the "large variables", and
check if referencing or copying is used.

Greetings from Frankfurt,

Fabian


lawrence
Guest
 
Posts: n/a
#5: Jul 17 '05

re: object references stored in array gives me what?


"Fabian Wleklinski" <Wleklinski.NNTP@eWorks.de> wrote in message news:<bmlk4e$je5@library1.airnews.net>...[color=blue]
> Hi Lawrence,
>
> if you handle large objects (> 250K) it would make sense to free
> some memory explicitely.
>
> Example given, after an operation like $text=addslashes($oldtext)
> you could free $oldtext using unset( $oldtext ), or alternatively
> using the same variable:
>
> $text = addslashes($text); // this works as well
>
> Search for all assignments concerning the "large variables", and
> check if referencing or copying is used.
>
> Greetings from Frankfurt,[/color]


Thank you, that is good advice. I will begin to use unset() to
explicitly recoup the memory allocated to no longer needed variables
and objects.
Closed Thread


Similar PHP bytes