jcrocker Posted October 2, 2008 Share Posted October 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/126769-solved-declaring-object-without-passing-to-a-variable/ Share on other sites More sharing options...
jcrocker Posted October 2, 2008 Author Share Posted October 2, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/126769-solved-declaring-object-without-passing-to-a-variable/#findComment-655654 Share on other sites More sharing options...
aschk Posted October 3, 2008 Share Posted October 3, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/126769-solved-declaring-object-without-passing-to-a-variable/#findComment-656243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.