Forums / Code Collaboration / PHP Class Destructors

PHP Class Destructors

Views: 3,121

Results 1 to 5 of 5
24 Nov 2021, 7:38 PM
#1
g2ktcf avatar

g2ktcf

Totally Zenned

Join Date:
Feb 2011
Location:
Lumberton, TX
Posts:
636
Plugin Contributions:
0

PHP Class Destructors

This is not really a ZC specific question but I am unsure of how this works in php.

I normally explicitly call destructors for other OOP languages. But in my Admin ZC tool, I only need the class while the tool is running (page is active). So I can instantiate the object when the page loads. However, I do not see any type of mechanism for knowing when the scope of the page is gone. All you have to do is switch to another page. I can register a shutdown function, but again, I am not sure when that executes. I know that the destructor should execute when nothing else references it...would that be the case if you move to another menu item in the Admin and leave that tool's page?

I do not want to end up with memory leaks. Any input is greatly appreciated.

Thanks!

25 Nov 2021, 1:36 AM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: PHP Class Destructors

g2ktcf:

This is not really a ZC specific question but I am unsure of how this works in php.

I normally explicitly call destructors for other OOP languages. But in my Admin ZC tool, I only need the class while the tool is running (page is active). So I can instantiate the object when the page loads. However, I do not see any type of mechanism for knowing when the scope of the page is gone. All you have to do is switch to another page. I can register a shutdown function, but again, I am not sure when that executes. I know that the destructor should execute when nothing else references it...would that be the case if you move to another menu item in the Admin and leave that tool's page?

I do not want to end up with memory leaks. Any input is greatly appreciated.

Thanks!
The nature of PHP is to load the "page" at each page load. If your class is only made active/accessible when your admin page is present (the page in question), then it would not be loaded (nor need destruction) when switching from one page to the next. PHP effectively ends when the page has completed loading. Now, if while loading a page the class was brought into existence and then was determined to definitely not be needed (or might get in the way) of further processing it perhaps would need to be destructed.

Another consideration for destruction would be if the class was loaded to a session instead of just being/becoming a class and/or assigned to a variable for use.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

25 Nov 2021, 1:45 AM
#3
g2ktcf avatar

g2ktcf

Totally Zenned

Join Date:
Feb 2011
Location:
Lumberton, TX
Posts:
636
Plugin Contributions:
0

Re: PHP Class Destructors

mc12345678:

The nature of PHP is to load the "page" at each page load. If your class is only made active/accessible when your admin page is present (the page in question), then it would not be loaded (nor need destruction) when switching from one page to the next. PHP effectively ends when the page has completed loading. Now, if while loading a page the class was brought into existence and then was determined to definitely not be needed (or might get in the way) of further processing it perhaps would need to be destructed.

Another consideration for destruction would be if the class was loaded to a session instead of just being/becoming a class and/or assigned to a variable for use.

that is exactly what I was concerned with. I need to rethink this a bit.

25 Nov 2021, 3:10 AM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: PHP Class Destructors

g2ktcf:

that is exactly what I was concerned with. I need to rethink this a bit.
So I'm not sure what the use case is for something that is to exist for a given page only to be created/recreated as a session variable (instead of say capturing information in post data or specific information within a session in general excluding the class itself), but if there is something that is being triggered to load the class if on a certain page, the else is to check if it is set and unset or destruct it if so...

Presumably the class is loaded through an auto_loader, which may call an init_includes or just do the class instantiation/destruction without the init_includes. This also depends on the load point(s) involved as session data is not all available all of the time.

Again, to me it seems a little backwards for a class to exist as a session only while a particular "page" is loaded where a session class allows carrying information and handlers across all pages basically for as long as the associated user is considered logged in.

If there is "trigger" data that is kept in the session, I would think that the class would simply be loaded when that page is loaded and the "settings" operated upon.

The above is from a perhaps misunderstanding of the expressed follow on concern.

Is there an "end point"? Well the expectation is that as one steps through all that is needed to load the index.php page, then generally there is a "bottom" to the page that is to be loaded, but understand that if thee is either an error on the page or some "redirection" in a middle file's load then anything desired to be loaded at the "end" doesn't get executed...

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

25 Nov 2021, 1:38 PM
#5
g2ktcf avatar

g2ktcf

Totally Zenned

Join Date:
Feb 2011
Location:
Lumberton, TX
Posts:
636
Plugin Contributions:
0

Re: PHP Class Destructors

I think it comes down to leaving the object fully instantiated the entire time at a Global level. I try to avoid Global variables/objects and was not thinking it through fully. You have explained it fully and I appreciate that.

This Etsy based project has several stages and each stage is easily defined as a method within class. So I can 1)Create the class through Auto load 2)Load the page through Admin menu. The approval process has several steps as well 3)Initial credential Request to Etsy 4) Receiving Key pair at a specific URL with GET from Etsy (asynchronous) 4)Using the keys to get a token from Etsy (all of this is an OAuth 2.0 process). 5) Customer configuration settings and finally 6) Retrieve inventory data from Etsy, 7) Get inventory data from ZC 8) Compare both to last transmitted values and 9)Update/reconcile both inventories.

So, I need to figure out the auto loader part of this and I was not expecting to have to do this. While this was not something I WANTED to do, I am sure in time I can figure this out.

I also found out that part of OAuth 2.0 is asynchronous. I am guessing that I need to do something like ZC does with IPNs from Paypal...basically write a specific handler that retrieves and stores the a key pair and give that to the other site as a URL. To be secure, I guess it would need to be simple, like simply write the values to a file? Is sanitization required? If so, to what level? Would it be possible to write these to the database without being a huge security risk? One item is in [A-Za-z0-9._~-] format so I can sanitize that easily enough. The other item is like a password and I am not sure of the format for it.