[UPHPU] Class and Object Storage

Alvaro Carrasco alvaro at epliant.com
Sun Feb 17 21:17:50 MST 2008


Justin Giboney wrote:
> This is my test page here.
>
> http://www.expresslanevideo.com/docs/guidtest2.php
>
> *Ignore the specific class and its functions (unless you want to know 
> more about it), it is just used as an example.
>
> This page, runs the GUID class function called getinstance. Since the 
> GUID class is set up as a singleton class, it should only be created 
> once, or every time it is unused for a period of time and deleted from 
> memory.
>
> In the __construct function of the class, I have put an echo that says 
> creating so that i know every time a new object is being created.
>
> The object is being created every time the page is refreshed or hit. I 
> don't want this to happen. I want to only have one object on the 
> system at a time, that is shared by all users.
>
> I ran a test and found out that JSP/Tomcat works the way that I want, 
> but since I like PHP, I want to be able to do this in PHP. How can 
> this be accomplish?
>
> Thank you,
>
> Justin Giboney
>
>
Since PHP is stateless by design, you cannot maintain an object alive 
past one request. In PHP, everything is created and then destroyed on 
every request. The only thing you can do is save the contents of the 
object (possibly by serializing it) and re-create it on the next 
request. Have you profiled the application? Instantiating a couple of 
objects is not generally an expensive operation (compared to other 
things), so is it really such a big problem to have object instantiated 
on every request.

If you really, REALLY want to keep an instantiated object across 
requests, do a google search for "php bananas" or "php script running 
machine". I don't know if it's usable though.

Alvaro


More information about the UPHPU mailing list