Jump to content

Recommended Posts

Hi all,

 

I was wondering how PHP deals with objects that are created without being passed to a variable:

 

class foo
{
public function construct()
{
	echo 'Object Created';
}
}

new foo();

 

The object is created, however does it remain in the memory or does garbage collection pick up nothing points to it?

 

Thanks for your time,

 

Jonathan

 

 

Sorry everyone, answered my own question, it does get removed straight away.

 

class foo
{
public function __construct()
{
	echo 'Object Created';
}
public function __destruct()
{
	echo 'Object Removed';
}
}

new foo();
new foo();

 

Thanks anyway!

 

 

i'm not 100% that it's garbage collection that is performing that for you.

I hypothesise that it's the lack of assignment that causes the created object to be immediately destructed.

 

A fairer test on garbage collection would be to create a variable (object) and never use it, and do lots of other things after it, and see when the object is destructed, and i think you'll find it's at the end of the script termination (as everything get's released from memory).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.