Jump to content

[SOLVED] Declaring object without passing to a variable


jcrocker

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.