有没有一种方法可以在php页面之间共享对象? [英] Is there a way to share object between php pages?

查看:108
本文介绍了有没有一种方法可以在php页面之间共享对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php的新手,但是在其他Web技术中,您可以在页面实例之间共享对象。例如,在java jsp页面中,您很容易拥有类,该类作为整个服务器实例的静态类存在。如何在php中做到这一点?

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 class for the whole server instance. How to do this in php?

我不是在指代会话变量(至少我不这么认为)。这更多是出于资源池的目的(也许是要共享的套接字,或数据库连接等)。因此,整个类需要在后续加载之间共享,而不仅仅是我可以在会话中存储的一些原始变量。

I am not refering to sessions variables (at least I don't think so). This is more for the purpose of resource pooling (perhaps a socket to share, or database connections etc). So a whole class needs to be shared between subsequent loads, not just some primitive variables that I can store in the session.

我也研究过制作php单例类,但我相信该类仅在同一页面内共享,而不是跨页面共享。

I have also looked into doing php singleton classes but I believe that class is only shared within the same page and not across pages.

为了使事情更加清楚,我正在寻找可以帮助我共享例如连接到connectSocket.php页面服务器的套接字的套接字,以便所有加载该页面的用户使用相同的套接字套接字,并且不会打开新的套接字。

To make things even more clear, I'm looking for something that can help me share, say, a socket connected to a server for a connectSocket.php page such that all users who loads that page uses the same socket and does not open a new one.

推荐答案

这是一个很难回答的问题,可能与您要找的不完全一样。

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

PHP是基于无共享架构构建的。如果需要在应用程序中使用某种类型的状态,则必须通过其他方式进行。

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.

首先,我建议您研究问题的核心。您真的需要它吗?如果您认为PHP应用程序可能会死亡(并丢失状态),那么丢失数据还可以吗?

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?

如果您必须保持状态,即使在应用程序死亡或其他情况下也是如此,您应该假设放置数据的最佳位置是MySQL。 PHP被用作业务逻辑的薄层,因此我强烈建议这样做。

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.

如果您不关心重启后丢失数据的问题,您要查找的域可能是缓存。我建议您调查 memcached ,或者如果您在单台计算机上,请 apc 。 APC绝对可以在一台机器上与Apache一起使用,但是假设您可能会丢失数据,您仍然必须对应用程序进行编码。

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.

如果您担心自己的数据,基础数据存储(MySQL)太慢,但是在重新启动后仍需要维护数据,您应该研究这两个系统的组合。您始终可以从缓存中推入和拉出数据,但只有在数据更新时才将其发送到Mysql。

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?

这篇关于有没有一种方法可以在php页面之间共享对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆