Destructing an object/Constructing

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Destructing an object/Constructing

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Destructing an object/Constructing

Post 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.
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Destructing an object/Constructing

Post by arjan.top »

php <5.3 does not have GC
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Destructing an object/Constructing

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Destructing an object/Constructing

Post 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.
(#10850)
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Destructing an object/Constructing

Post by swraman »

THanks everyone, that cleared up a lot of things.
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Destructing an object/Constructing

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Destructing an object/Constructing

Post by Christopher »

Depends on the OS. It may just start swapping memory to disk and run very slowly.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Destructing an object/Constructing

Post 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 ).
Post Reply