This is a discussion on Problem with sessions (in global scope vs class scope) within the PHP Language forums, part of the PHP Programming Forums category; Hello, i'me having a wierd problems with sessions. PHP 4.3.3, Register globals is on, and the sessions ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello, i'me having a wierd problems with sessions.
PHP 4.3.3, Register globals is on, and the sessions module is installed. if i have a page like this: <? session_start(); $_SESSION[ 'color' ]="blue"; ?> On the next page i can see that $_SESSION[ 'color' ] is actually equal to "blue". So i know that my sessions work in global scope. Now i tried to make a nice little session wrapper to help in the code development. Using this wrapper, for some odd reason, it doesn't work. I posted the class after this text for verification. Did i do a stupid mistake somewhere? OR am i missing something? Thanks for all your help! Php4evr! :) ---------------- <?php class SessionWrapper{ function SessionWrapper( $params = array() ){ session_start(); } /** * Gets a session variable, for example, * $session->getValue( "FavoriteColor" ) would return "blue". Objects are even serialized. * so you can set a session string, integer, object, array, whatever. It will be * serialized by the session API. */ function getValue( $varName ){ global $_SESSION; return $_SESSION[ $varName ]; } /** * Sets a session variable, for example, * $session->setValue( "FavoriteColor", "blue" ) would set the * "FavoriteColor" session variable to "blue". Be carefull not to write over eachother's * session variables.Objects are even serialized. * so you can set a session string, integer, object, array, whatever. It will be * serialized by the session API. */ function setValue( $name, $value ){ global $_SESSION; $_SESSION[ $name ] = $value; } } |
|
|||
|
This is under a project for a "Software Engineering" class where we learn
about desing methodologies so i would like to have proper desing. Since the rest of the app is done in an OO aproach, i think specifing this entity in its own class is probably a good idea. You can imagine the day that you want to add extra pre/post operations when setting sessions, that could be done easily if it waer a class. But if you simply do $_SESSION[ 'foo' ] =.. Then you can't do that handling. Same thing for collision or any other extra handling you would want to do, etc... That did the trick! THANKS ALOT!!!! I guess global $foo; if $foo is superglobal created a new variable called $foo that wasen't instantiated.. Wow.. Here's a beer. |_|D It's gotta be cause we have the same first name :) (but mine is french hehe) TTYL! "André Næss" <andrena.spamreallysucks@ifi.uio.no> wrote in message news:bp7891$bnd$1@maud.ifi.uio.no... > Yoyoma_2: > > > Hello, i'me having a wierd problems with sessions. > > PHP 4.3.3, Register globals is on, and the sessions module is installed. > > > > Now i tried to make a nice little session wrapper to help in the code > > development. Using this wrapper, for some odd reason, it doesn't work. > > > > > function getValue( $varName ){ > > global $_SESSION; > > $_SESSION is a superglobal, and there is no need for the global statement. > > I'm not sure if this is why it doesn't work. But I must say your class looks > completely meaningless to me, what benefit does it provide? Why do you need > it? Why is > > $sessionWrapper->setValue('key', 'value'); > > better than > > $_SESSION['key'] = 'value'; > > ? > > André Næss > > |
|
|||
|
Yoyoma_2:
> Hello, i'me having a wierd problems with sessions. > PHP 4.3.3, Register globals is on, and the sessions module is installed. > > Now i tried to make a nice little session wrapper to help in the code > development. Using this wrapper, for some odd reason, it doesn't work. > > function getValue( $varName ){ > global $_SESSION; $_SESSION is a superglobal, and there is no need for the global statement. I'm not sure if this is why it doesn't work. But I must say your class looks completely meaningless to me, what benefit does it provide? Why do you need it? Why is $sessionWrapper->setValue('key', 'value'); better than $_SESSION['key'] = 'value'; ? André Næss |
|
|||
|
"Yoyoma_2" <DontAnswer@somewhere.ca> schrieb:
> function getValue( $varName ){ > global $_SESSION; > return $_SESSION[ $varName ]; > } I dont' really believe that it's the reason for the error, but you dont't habe to define $_SESSION as global. Regards, Matthias |
|
|||
|
That was the error Thanks!
"Matthias Esken" <muelleimer2003nospam@usenetverwaltung.org> wrote in message news:bp7j3b.1ag.1@usenet.esken.de... > "Yoyoma_2" <DontAnswer@somewhere.ca> schrieb: > > > function getValue( $varName ){ > > global $_SESSION; > > return $_SESSION[ $varName ]; > > } > > I dont' really believe that it's the reason for the error, but you > dont't habe to define $_SESSION as global. > > Regards, > Matthias |
|
|||
|
.... as $_SESSION is always global
Savut "Matthias Esken" <muelleimer2003nospam@usenetverwaltung.org> a écrit dans le message de news:bp7j3b.1ag.1@usenet.esken.de... > "Yoyoma_2" <DontAnswer@somewhere.ca> schrieb: > > > function getValue( $varName ){ > > global $_SESSION; > > return $_SESSION[ $varName ]; > > } > > I dont' really believe that it's the reason for the error, but you > dont't habe to define $_SESSION as global. > > Regards, > Matthias |