php_dave Posted August 13, 2008 Share Posted August 13, 2008 Guys, I had a quick hunt around and couldnt find anything specific - and have read the documentation but couldnt find anything concrete. At what point is an instance destroyed from memory without impicitly calling unset($class) or $class = null; I ask because I am relatively new to OOP and have just implemented a class to handle some SLA logic - this logic is used across three seperate .php pages and I initiate a new instance on each page. Im a little worried that im not managing the destruction and removal from memory effeciently and will end up with slow performance or memory fill! Questions: 1. Should I be initiating a new instance of my class on every page that uses it? 2. Do I need an implicit unset($class) and a __destructor or is page end good enough to destroy from memory? Sorry if I am rehashing old ground but you guys are so helpful Link to comment https://forums.phpfreaks.com/topic/119580-solved-object-in-memory/ Share on other sites More sharing options...
DarkWater Posted August 14, 2008 Share Posted August 14, 2008 Garbage collection is done at the end of the request as far as I know. Link to comment https://forums.phpfreaks.com/topic/119580-solved-object-in-memory/#findComment-616101 Share on other sites More sharing options...
genericnumber1 Posted August 14, 2008 Share Posted August 14, 2008 garbage collecting is done, when a function ends, when the script finishes, and when you tell php to do it with unset or mysql_free_result. as for your question, variables will pass between includes, so no, you don't have to re-initialize it. and also, no, you don't have to unset variables specifically or use destructors this is of course a big jump from servlets and javaserver pages if that's what you're typically use to using. Link to comment https://forums.phpfreaks.com/topic/119580-solved-object-in-memory/#findComment-616119 Share on other sites More sharing options...
DarkWater Posted August 14, 2008 Share Posted August 14, 2008 garbage collecting is done, when a function ends, when the script finishes, and when you tell php to do it with unset or mysql_free_result. as for your question, variables will pass between includes, so no, you don't have to re-initialize it. and also, no, you don't have to unset variables specifically or use destructors this is of course a big jump from servlets and javaserver pages if that's what you're typically use to using. I meant garbage collection for variables that weren't unset and any objects destructors that weren't already used are called at the end of the request. Link to comment https://forums.phpfreaks.com/topic/119580-solved-object-in-memory/#findComment-616120 Share on other sites More sharing options...
php_dave Posted August 14, 2008 Author Share Posted August 14, 2008 Thanks for the clarification guys! Link to comment https://forums.phpfreaks.com/topic/119580-solved-object-in-memory/#findComment-616312 Share on other sites More sharing options...
448191 Posted August 14, 2008 Share Posted August 14, 2008 It should be noted that cross-referencing instances will cause the garbage collector not to clean up either instances, even if all references are out of scope. Link to comment https://forums.phpfreaks.com/topic/119580-solved-object-in-memory/#findComment-616315 Share on other sites More sharing options...
ignace Posted August 14, 2008 Share Posted August 14, 2008 1. Should I be initiating a new instance of my class on every page that uses it? use a singleton, but only when you want to work with it in multiple scopes 2. Do I need an implicit unset($class) and a __destructor or is page end good enough to destroy from memory? pear uses a destructor which calls all child destructors, the only problem here is that your classes have to implement the parent class the parent destructor is then registered with the shutdown function i think, so one destructor starts the destruction of all registered objects Link to comment https://forums.phpfreaks.com/topic/119580-solved-object-in-memory/#findComment-616363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.