Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store non-serializable, process-specific objects between PHP requests?

Tags:

php

sockets

web

We have a need to keep a collection of socket objects around that are associated with different client browser sessions, so that when the client's browser makes a subsequent request, we can use the existing socket connection/session to make a request on their behalf. The socket is to something that is not HTTP. Is there a way to store objects like this in PHP that will survive across page requests?

like image 963
Keith Hill Avatar asked Apr 13 '17 00:04

Keith Hill


1 Answers

Is there a way to store objects like this in PHP that will survive across page requests?

No.

To quote zombat's answer to a very similar question:

In PHP, there is no concept of page instances. Every request to the web server is a fresh start. All the classes are re-loaded, so there is no concept of class sharing, nor is there a concept of resource pooling, unless it is implemented externally. Sharing sockets between web requests therefore isn't really possible.

If the objects were serializable, you could use PHP's serialize() and unserialize() in conjunction with MySQL memory tables to solve this issue. Other than that, I don't think there's much else you can do.

like image 155
Amal Murali Avatar answered Sep 30 '22 01:09

Amal Murali