Destructing an object/Constructing
Moderator: General Moderators
Destructing an object/Constructing
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
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
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.
__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
php <5.3 does not have GC
Re: Destructing an object/Constructing
<5.3 did have GC, it was just that GC couldn't dispose variables trapped in reference cycles.arjan.top wrote:php <5.3 does not have GC
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Destructing an object/Constructing
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.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?
(#10850)
Re: Destructing an object/Constructing
THanks everyone, that cleared up a lot of things.
Re: Destructing an object/Constructing
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
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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Destructing an object/Constructing
Depends on the OS. It may just start swapping memory to disk and run very slowly.
(#10850)
Re: Destructing an object/Constructing
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 ).