Is there a way to share object between php pages?

后端 未结 5 1145
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 22:17

I am new to php, but in other web technologies, you can share objects between page instances. For example, in java jsp pages you easily have on class that exist as static cl

5条回答
  •  长情又很酷
    2020-12-14 22:35

    This is a bit of a difficult answer, and might not be exactly what you are looking for.

    PHP is built upon a 'shared-nothing' architecture. If you require some type of state across your application, you must do this through other means.

    First I would recommend looking into the core of the problem.. Do you really need it? If you assume the PHP application could die (and lose state) is it ok to lose the data?

    If you must maintain the state, even after the application dies or otherwise, you should assume probably the best place to put the data is in MySQL. PHP is intended as a thin layer around your business logic, so I can highly recommend this.

    If you don't care about losing the data after a restart, the problem domain you're looking for is probably caching. I would recommend looking into memcached or if you're on a single machine, apc. APC will definitely work for you with Apache on a single machine, but you will still have to code your application assuming you might lose the data.

    If you're worried your underlying datastore (MySQL) is too slow, but you still need to maintain the data after a restart, you should look into a combination of these 2 systems. You can always push and pull your data from the cache, but only when it updates send it over to Mysql.

    If the data is purely user or session-bound, you probably want to just looking into the sessions system.

    I've personally developed a reasonably large multi-tenant application, and although its a pretty complex application, I've never needed the true state you're looking for.

    Update: Sorry, I did not read your note about sharing a socket. You will need a separate daemon to handle this, perhaps if you can explain your problem further, there might be other approaches. What type of socket is this?

提交回复
热议问题