Jump to content

Recommended Posts

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

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.

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.

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

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.