使用 PHP 进行面向对象编程:刷新杀死我的对象

发布于 2024-08-22 07:53:14 字数 187 浏览 10 评论 0 原文

我一直在研究 PHP 的 OOP,我注意到一些事情......每次刷新页面时都会重新实例化对象。问题是我希望对象在某人访问网站的整个过程中将某些信息保留在类变量中。

  1. 有什么方法可以保持 对象一直活着 有人在网站上冲浪吗?
  2. 除了我的之外还有什么替代方案 问题?

如果有例子也会很有帮助!

I have been poking around in PHP for OOP and I noticed something... Objects are re-instantiated each time the page is refreshed. The problem is that I want the object to keep certain information in class variables for the whole time that someone is on a website.

  1. Is there some sort of way to keep an
    object alive the whole time that
    someone is surfing on the website?
  2. What alternatives are there to my
    problem?

It would be really helpful to have example too!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

独守阴晴ぅ圆缺 2024-08-29 07:53:15

您可以使用会话在不同页面之间保存与某个用户关联的数据 (引用)

PHP 中的会话支持包括
跨区域保存某些数据的方法
后续访问。

有关会话的更多信息,请参阅手册的会话处理部分。

You can use Sessions to keep data associated to one user between different pages (quoting) :

Session support in PHP consists of a
way to preserve certain data across
subsequent accesses.

See the Session Handling section of the manual, for more informations about sessions.

红玫瑰 2024-08-29 07:53:15

PHP 没有状态。每个页面加载都是一次性事件。您可以使用会话或通过将信息存储在数据库中来保留数据。

PHP isn't stateful. Every page load is a one time event. You can persist data with sessions, or by storing information in a database.

甜味拾荒者 2024-08-29 07:53:15

php 脚本必须先退出 apache 才能提供页面服务,所以如果您确实想这样做,您可以做的一件事是 序列化并存储您想要保留并使用的所有对象 cookie

A php script has to exit before apache can serve the page, so if you really want to do that, one thing you can do is serialize and store all the objects that you want to persist and use session cookies to keep track of the users

時窥 2024-08-29 07:53:15
  1. PHP 不是有状态的,每个请求都是服务器上的一个新进程。

您最好的选择是使用会话数据,并在实例化对象时将会话数据传递给对象。让构造者从会话中提取他们需要的数据,您基本上就可以获得所需的状态完整性。

您可以使用访问会话,

$_SESSION['stuff'] = $data;

然后您可以使用您的对象,例如
$x = new DataStore($_SESSION['stuff']);

如果会话中有数据,该对象将从该数据中填充自身。否则它将默认为其标准 init。

  1. PHP isn't statefull every request is a new process on the server

Your best bet is to use session data and hand the session data to the objects when you instantiate them. Have the contructors pull the data they need out of the session, and you'll essentially have the state fullness you need.

you can acess sesion using

$_SESSION['stuff'] = $data;

then you can use your objects like
$x = new DataStore($_SESSION['stuff']);

if theres data in the session the object will populate itself from that data. Otherwise it will default to its standard init.

弄潮 2024-08-29 07:53:15

即使序列化对象然后反序列化之类的方法很有用,您也必须确保首先了解对象“消失”的原因。

HTTP 是用于从 Web 服务器检索页面和其他资源的协议,它是无状态的。这基本上意味着一个请求与另一个请求一无所知,即使它来自同一用户。这样想,当您请求 PHP 页面时,脚本就会运行,完成后 Apache 会将结果发送给您。当您再次请求该页面时,它会执行与您第一次执行相同的操作。它是无国籍的。

有一些技术可以在请求之间保持状态(使其不会忘记您的对象),并且涉及 cookie 或 URL 重写等技术。但在开发 Web 应用程序时,您必须牢记 HTTP(以及 PHP 脚本)的无状态本质。

Even when approaches like serializing objects and then deserializing them is useful, you have to make sure you understand first why your objects "disappear".

HTTP, the protocol used to retrieve pages and other resources from Web servers, is stateless. It basically means one request knows nothing from another request, even when it came from the same user. Think of it this way, when you request your PHP page, the script is run and after it finishes Apache sends out the result to you. When you request the page again, it does the same thing as if it was the very first time you did it. It's stateless.

There are techniques to keep state between requests (make it to not forget your objects) and those involve things like cookies or URL rewriting. But you have to keep in mind the stateless nature of HTTP (and thus your PHP script) when developing Web applications.

冰葑 2024-08-29 07:53:15

会话很好,我在一些 PHP 编程中使用它们来保存对象状态。

或者更好的解决方案是使用 Flex 这样你就不必担心无状态 HTTP 协议......

SESSIONS are good, i use them to hold object state in some of my PHP programming.

Or a better solution would be to use Flex so you don't have to worry about the stateless HTTP protocol...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文