Hi,
I'm new to PHP programming (coming from C++); and all is well, except when
it comes to memory management. Particularly, with passing objects/arrays in
and out of functions. I've had so many unexpected results because of this
feature, that I'm almost pissed off, almost.
Things like, I have a class, with some callbacks (which default to functions
inside the class). But when the default functions were called, $this
couldn't see the values of the member variables. I found that my calling
array($this, "fun") (for use in a call to call_user_func) was creating a
copy of $this and thus it was a copy of the variables. Fixed with a $cb =
array(); $cb[0] = &$this; etc...
Later, with more problems, I found that $x = new y; made two copies (I've
assumed from the testing I'd done). One where it calls the constructor, and
then one when it copies it into $x. Fixed with a $x = &new y; (I consider
myself lucky to have discovered this as it could very easily have gone
unnoticed, had I not been setting up a reference to this in the constructor)
Which makes me worry that there are other places I don't know about!
....and lots more of course...
I guess with a little more care, it will be obvious where a problem exists,
but as I am a newbie, I know it will continue to destroy me.
Are there any tools or memory management functions that will allow me to see
exactly how many copies of various objects/classes/strings I have. Or a way
to dump a memory allocation when it happens or anything. I'm fearful that
there may be millions of copies of my classes and such that I won't know
about until it gets released - and two users go to the web site at the same
time and cause the server to run out of memory.
C++ is so much easier, I know exactly what memory is allocated, when and
where, and when it will be destroyed. Whoever invented garbage collection
and all that; a can of comeuppance upon them (not the good kind).
Please, any information will be great.
Thank you
MRe
I'm new to PHP programming (coming from C++); and all is well, except when
it comes to memory management. Particularly, with passing objects/arrays in
and out of functions. I've had so many unexpected results because of this
feature, that I'm almost pissed off, almost.
Things like, I have a class, with some callbacks (which default to functions
inside the class). But when the default functions were called, $this
couldn't see the values of the member variables. I found that my calling
array($this, "fun") (for use in a call to call_user_func) was creating a
copy of $this and thus it was a copy of the variables. Fixed with a $cb =
array(); $cb[0] = &$this; etc...
Later, with more problems, I found that $x = new y; made two copies (I've
assumed from the testing I'd done). One where it calls the constructor, and
then one when it copies it into $x. Fixed with a $x = &new y; (I consider
myself lucky to have discovered this as it could very easily have gone
unnoticed, had I not been setting up a reference to this in the constructor)
Which makes me worry that there are other places I don't know about!
....and lots more of course...
I guess with a little more care, it will be obvious where a problem exists,
but as I am a newbie, I know it will continue to destroy me.
Are there any tools or memory management functions that will allow me to see
exactly how many copies of various objects/classes/strings I have. Or a way
to dump a memory allocation when it happens or anything. I'm fearful that
there may be millions of copies of my classes and such that I won't know
about until it gets released - and two users go to the web site at the same
time and cause the server to run out of memory.
C++ is so much easier, I know exactly what memory is allocated, when and
where, and when it will be destroyed. Whoever invented garbage collection
and all that; a can of comeuppance upon them (not the good kind).
Please, any information will be great.
Thank you
MRe
Comment