Nils-H Posted August 9, 2013 Share Posted August 9, 2013 Having a Java background, thread safety is something I usually consider with my code. So I'm wondering how this works with PrestaShop/PHP. Are modules thread safe? That is, will each request have a new instance of the module, so I can safely use instance variables to keep state through a request? Or should I not keep any state in module instance variables? Link to comment Share on other sites More sharing options...
bellini13 Posted August 9, 2013 Share Posted August 9, 2013 This is really a question regarding the server configuration, not really specific to Prestashop. You could read through this question, and you would probably do better with a google search for "php thread safe" http://stackoverflow.com/questions/1623914/what-is-thread-safe-or-non-thread-safe-in-php Link to comment Share on other sites More sharing options...
Burhan BVK Posted August 9, 2013 Share Posted August 9, 2013 They are thread safe since 1.4, but earlier versions were not. But overall thread safety is rarely causes any issues in PHP and prestashop, as each request to the server is contained within a single thread. You need to think in terms of requests and sessions instead of threads. Modules in Prestashop instantiated trough the module class getinstancebyname method. In the current version the same instance would be used as long as this method is used, so you can use module variables in different parts of your code. But if you are too converned you can always use static variables. Link to comment Share on other sites More sharing options...
Nils-H Posted August 9, 2013 Author Share Posted August 9, 2013 Yes, I realize that this is dependent on server configuration, but I was thinking generally, and in the case where threading is enabled. Although thread safety was maybe not what I was looking for anyway, but rather if there was a shared instance of a module in the runtime, or if it is created per request. If it is created per request, then using instance variables would be safe, so if that's the case, then everything is ok. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now