Page 1 of 1

Destructing an object/Constructing

Posted: Mon Jul 27, 2009 11:44 pm
by swraman
Hi,

I have a class (call it main) defined, and in it are variables that point to instances of other classes.

If I have an instance of this main class (which in turn has instances of other classes its vars point to), and I __destruct the instance of the main class; what happens to the other objects that main's variables pointed to? do they get destructed as well, or are they garbage collected and do they still take up memory on the server until the script is finished executing?

Also, say Im writing something that creates many instances of main (in a for loop, a new instance each iteration). Would it be better to destruct the object at the end of every iteration? does it even make a difference? If I dont destruct it, does it just get garbage collected; and is garbage collection the same as destructing?

And thirdly about constructors: are constructors automatically called when a new object is created? does it work in the same way that Java uses constructors, where you can have more than one (as long as their input vars are different)?

Thanks

Re: Destructing an object/Constructing

Posted: Tue Jul 28, 2009 1:10 am
by Darhazer
PHP uses reference counter, so if there any other references to the same instances, the main class is referring to, they won't be destructed.

__construct() is called every time you are using the new operator. Unlike other languages, you cannot have different constructors, because PHP does not support method overloading.

Re: Destructing an object/Constructing

Posted: Tue Jul 28, 2009 3:36 am
by arjan.top
php <5.3 does not have GC

Re: Destructing an object/Constructing

Posted: Tue Jul 28, 2009 4:11 am
by Weirdan
arjan.top wrote:php <5.3 does not have GC
<5.3 did have GC, it was just that GC couldn't dispose variables trapped in reference cycles.

Re: Destructing an object/Constructing

Posted: Tue Jul 28, 2009 9:51 am
by Christopher
swraman wrote:Also, say Im writing something that creates many instances of main (in a for loop, a new instance each iteration). Would it be better to destruct the object at the end of every iteration? does it even make a difference? If I dont destruct it, does it just get garbage collected; and is garbage collection the same as destructing?
In general in PHP you don't destroy variables because everything is destroyed when the script exits. The memory manager will not deallocate memory when you unset variables because it allocates large memory blocks. It may reuse memory it has allocated, so in some cases you may want to unset a big array of data if you are going to allocate another big array of data. But you are not guaranteed that the memory will actually be reused. You cannot control that.

Re: Destructing an object/Constructing

Posted: Tue Jul 28, 2009 2:57 pm
by swraman
THanks everyone, that cleared up a lot of things.

Re: Destructing an object/Constructing

Posted: Wed Aug 05, 2009 11:24 pm
by swraman
So what would happen if the allocated memory for PHP runs out (I believe default is 8MB, I know its unlikley).

Will it give an overflow error? Say a script creates thousands of objects during its execution, and its half way through the execution and needs more memory to run. Will it just overwrite and objects that no longer have anything pointing to them?

Thanks

Re: Destructing an object/Constructing

Posted: Thu Aug 06, 2009 1:11 am
by Christopher
Depends on the OS. It may just start swapping memory to disk and run very slowly.

Re: Destructing an object/Constructing

Posted: Sat Aug 08, 2009 12:02 pm
by josh
You'll get an error memory_limit exceeded, unless the memory is exceeded by stack space in which case you will get a nice blank page after 30 seconds or so usually ( if you invoked a recursive call stack for instance ).