I'm using the Zend HTTP client to implement a web client for communicating with a third-party service. I was thinking about making it a singleton pattern so each call could take advantage of using cookies from the service, so that we wouldn't need to re-login every time we instantiate a new version of the client.
The client will have multiple classes communicating with it, possibly from several different locations in code trying all at once. I'm worried that having a singleton client will cause race conditions when multiple entities try to use it at once.
Will this be an issue with the singleton pattern in a PHP HTTP client? If so, is there any other way to have the benefit of a Zend HTTP client that can store and use cookies across multiple transactions without having these issues?
Since PHP is not multithreaded, this alone shouldn't be able to cause a race condition.
It would be possible if there are multiple requests at the same time and you're using a shared resource (e.g. a file on your server). In that case you would need to have proper locking in place, such as flock()
.
You could also store cookies per session, i.e. shared-nothing architecture; that should solve the problem as well.