![]() |
Singleton in Session variable?
How can I store a singleton in a session variable, and only change properties if $_GET['lang'] is set?
|
php Code:
?? |
Thanks, I'll check it out.
|
Nope, variable still gets reset on every request. The singleton gets reinstanciated every freakin' time on the roundtrip to the server.
Simply cannot see how the singleton design pattern is of ANY use in a 'web language' as PHP. |
I think there is a fundamental misunderstanding of this pattern in PHP and its many uses.
A singleton is a class where only one object can created from it, thus making the object of that type a 'singleton' (as its the only one allowed to be created). This allows you to make sure that only one version of that object is alive at that particular execution, of course it will have to be created again when the next request happens. In the example given above, the session variable will be over-written on each execution because you don't make the check to see if it already exists, so every request it will be over-written. Something like this will work: PHP Code:
I still don't quite get why you would need this though, most of my singletons are only for that execution and any variables i need saving between page views just go into a namespace'd session via my session wrapper, I very rarely need to save objects to a session because of the way my system works. |
I wouldn't recommend storing the singleton itself within the global session space as this will eventually lead to excessive server loading with all of the singleton data ....
The easier thing to do here is store only the configuration you are using for the Singleton and then recreate it on the next page load If this is impossible as you are storing to much information in your singleton, then you need to rethink your code structure as it is flawed because you are relying heavily on data that may not be 100% accurate |
I agree, like I said in my post, I have rarely if ever needed to store a full object into a session variable.
|
| All times are GMT. The time now is 11:07 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0