有没有办法在php页面之间共享对象?
我是 php 新手,但在其他 Web 技术中,您可以在页面实例之间共享对象。 例如,在 java jsp 页面中,您可以轻松地拥有作为整个服务器实例的静态类存在的类。 如何在 php 中做到这一点?
我不是指会话变量(至少我不这么认为)。 这更多是出于资源池的目的(可能是共享套接字,或数据库连接等)。 因此,整个类需要在后续加载之间共享,而不仅仅是我可以在会话中存储的一些原始变量。
我也研究过 php 单例类,但我相信该类仅在同一页面内共享,而不是跨页面共享。
为了让事情变得更清楚,我正在寻找可以帮助我共享连接到 connectSocket.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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
大多数 PHP 数据库库已经使用连接池。 例如,您可以像请求新连接一样调用 pg_connect,但如果连接字符串与已存在的连接相同,您将取回已建立的连接。 如果您只关心数据库访问的池化,那么您只需确认它存在于您正在使用的数据库库中即可。
Most of the PHP database libraries use connection pooling already. You call, for example, pg_connect as if you were requesting a new connection, but if the connection string is the same as a connection that already exists, you will get the established connection back instead. If you only care about pooling for database access, then you can just confirm that it exists in the db library you're using.
另一个可怕的解决方案可能是将对象的数据加载到任何 $_SESSION 变量,然后将其重新使用到其他页面的对象中。
事实上,这是我将在项目中遵循的解决方案,直到我找到更好的解决方案。
问候!
An other horroble solution may be to load the data of the object to any $_SESSION variable and then user it back into the object of the other page.
In fact, this is the solution I'm going to follow in my project, until I get some better one.
Regards!
这可能是部分答案,但您可以将类的实例保存到会话变量中并在其他时间访问它。
This is likely a partial answer but you can save an instance of a class into a Session variable and access it at another time.
Web 服务的 Java 和 Web 服务的解释语言(如 PHP 和 Perl)之间存在根本区别。 在 Java 中,您的 Web 服务器将有一个维护状态的操作环境(即 Tomcat)。 对于解释型语言,对 Web 服务器的请求通常会生成一个新的 Web 服务器线程,该线程又为该线程加载新的操作环境,在本例中为 PHP 环境。
因此,在PHP中,没有页面实例的概念。 对网络服务器的每个请求都是一个新的开始。 所有的类都会重新加载,所以不存在类共享的概念,也不存在资源池的概念,除非是外部实现的。
因此,在 Web 请求之间共享套接字实际上是不可能的。
There's a fundamental difference between web-served Java and web-served interpreted languages like PHP and Perl. In Java, your web server will have an operating environment that maintains state (ie. Tomcat). With interpreted languages, a request to your web server will generally spawn a new web server thread, which in turn loads a fresh operating environment for that thread, in this case, the PHP environment.
Therefore, 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.
这是一个有点困难的答案,可能不正是您正在寻找的答案。
PHP 建立在“无共享”架构之上。 如果您需要在应用程序中使用某种类型的状态,则必须通过其他方式来实现。
首先,我建议调查问题的核心。您真的需要它吗? 如果您假设 PHP 应用程序可能会死掉(并丢失状态),是否可以丢失数据?
如果您必须维护状态,即使在应用程序终止或其他情况之后,您也应该假设放置数据的最佳位置可能是在 MySQL 中。 PHP 旨在作为业务逻辑周围的一个薄层,因此我强烈推荐它。
如果您不关心重新启动后丢失数据,那么您正在寻找的问题域可能是缓存。 我建议您查看 memcached 或者如果您在一台计算机上,apc。 APC 肯定适合您在单台机器上使用 Apache,但假设您可能会丢失数据,您仍然需要编写应用程序代码。
如果您担心底层数据存储 (MySQL) 太慢,但重新启动后仍需要维护数据,那么您应该考虑这两个系统的组合。 您始终可以从缓存中推送和提取数据,但只有当数据更新时才会将其发送到 Mysql。
如果数据纯粹是用户或会话绑定的,您可能只想查看会话系统。
我亲自开发了一个相当大的多租户应用程序,尽管它是一个相当复杂的应用程序,但我从来不需要您正在寻找的真实状态。
更新:抱歉,我没有阅读您有关共享套接字的说明。 您将需要一个单独的守护进程来处理此问题,也许如果您可以进一步解释您的问题,可能还有其他方法。 这是什么类型的插座?
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?