Re: Re: Session variables are not stored when set in implicitly calledconstructor!??

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hallo again,
thank You for Your response.

> >
> > // singleton for request
> > class Request {
> >     function __destructor() {
> >         $_SESSION["variable"] = "hallo";
>
> The __destructor() method is supposed to be about killing the class
> (Request).  It's probably bad practice to be changing $_SESSION vars
> inside of there even if that $_SESSION var affects your class.  Here,
> read this page and maybe it will make more sense to you:
> http://php.net/manual/en/language.oop5.decon.php

i agree that in *most* cases destructor should only clean up after object.
unfortunatly for "request" class there are few requirements:
1) must allow immidiate use w/o any explicit initialization or finalizaion.
e.g. at any file You can use only Request::getInstance()->isRefresh() /**
test if this request was caused by browser refresh */. this should be one
and only line (except for inclusion of request.php oc) referencing request
class -- no explicit call to Request::saveToSession() or anything ugly and
potentialy disastrous like that.
2) must provide mechanism for generating per session uniqe request
identiers. now implemented as md5($_SESSION["next_reqest_id"]++) not very
effective but handy:-P
3) must be serializable (this is why i can not put that session manipulation
at __sleep function)

these requirements (1,3) cast out __destruct, __sleep and explicit call to
some saveToSession function. do You have any other idea when to store this
information , please?

> Heck, look at the user contributed notes since one of them will directly
> affect this idea of yours.

*doh* i don't know how could i coverlooke taht user reaction:-( thank You.

> If the reference count for $req is greater than 0 (i.e. there is a
> Singleton) then __destruct won't be called.  And why should it be
> called?  Because, again, __destruct is for cleaning up the class while
> the object is getting destroyed.

Isn't __destruct is called even if reference count is gr then zero as a part
of script finalization? imo yes -- i tried this with echoing something
within singletons destructor (no invoke with explicit delete:-) and it
worked.


BR
a3c


----- Original Messages ----- 
> Hallo everybody,
> hope I am writing to correct mailinglist(^_^*)...

Absolutely!

> I have troubles with sessions and descructor in php5. Can not set session
> variable in destructor when it's called implicitly. Do You know solution
> please?
> I think problem is that session is stored before imlicit object
destruction
> *doh*
>
> example.php:
> <?php
> session_start();
> session_register("variable");

*Note* You don't need to use session_register() if you use $_SESSION.
In fact this probably isn't even doing what you think it is.  Using
$_SESSION indexes is the preferred way to go about using sessions.

>
> // singleton for request
> class Request {
>     function __destructor() {
>         $_SESSION["variable"] = "hallo";

The __destructor() method is supposed to be about killing the class
(Request).  It's probably bad practice to be changing $_SESSION vars
inside of there even if that $_SESSION var affects your class.  Here,
read this page and maybe it will make more sense to you:
http://php.net/manual/en/language.oop5.decon.php
Heck, look at the user contributed notes since one of them will directly
affect this idea of yours.

>     }
> }
>
> $req = Request::getInstance();
> $req->doSomeThink();
> echo "This should be hallo 2nd time: " . $_SESSION["variable"];    //
> unfortunatly this is not set
> echo " <a href='example.php'>Click and cry;-(</a>";
> // implicit desturctor call

If the reference count for $req is greater than 0 (i.e. there is a
Singleton) then __destruct won't be called.  And why should it be
called?  Because, again, __destruct is for cleaning up the class while
the object is getting destroyed.

> ?>

Attachment: smime.p7s
Description: S/MIME cryptographic signature


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux