PHP如何跟踪创建的对象? [英] How does PHP track created objects?

查看:64
本文介绍了PHP如何跟踪创建的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是我并不是来自OOP的背景,尽管我在阅读和学习的过程中仍在为一些概念而苦苦挣扎.

This may be a bit of daft question, but I don't come from an OOP background and although I'm reading and learning as I go I'm still struggling with a few concepts.

现在,我正在使用PHP 5.3,并使用一些不同的对象类设计了一个相当简单的登录名:User,它定义了用户.启动和维护会话数据以及是否有人登录的会话,以及执行查询的数据库.

Right now I'm working with PHP 5.3 and desiging a fairly simple login using a few different object classes: User which defines the user. Session which starts and maintains session data and if someone is logged in, and Database which does the queries.

因此,当我运行脚本时,实例化我的会话对象,依此类推……这是我的问题.当我从一页移到下一页时,如何跟踪该对象?或者也许我的问题是PHP如何知道与我的登录相关的对象是我的而不是其他登录该站点的人?

So when my script is run, my session object is instantiated, etc... here's my problem though. When I move from one page to the next how is that object tracked? Or perhaps rather my question is how does PHP know the objects that relate to my login are mine and not someone else who logged into the site?

我知道我是否以非OOP方式进行操作,我只需检查每个页面上的会话cookie并以这种方式检查我的数据,这是我的大脑可以处理的很好的方法.但是,在何处以及如何跟踪对象数据.

I know if I'm doing this in a non OOP way, I'd simply check the session cookie on each page and check my data that way, which is fine my brain can handle that. But, where and how is object data tracked.

EG: 在每个页面上,我要检查是否有人登录,请参考$ session-> is_logged_in()等is_logged_in检查私有变量名称是true还是false. 目前还没有检查cookie,这意味着该对象仍然存在,并且由于不断询问存储变量,因此该实例必须持续有用...但是无论服务器如何绑定该对象的实例,PHP都将如何工作?给那个用户?如果所有这些对象都漂浮在服务器上,直到我销毁它们,这些对象会不会使用大量内存?

EG: On each page I want to check if someone is logged in I reference $session->is_logged_in() etc is_logged_in checks the a private variable name is true or false. There's no checking of cookies at this point which means this object still exists, and, as it keeps asking for a stored variable the instance must persist to be useful... but how does PHP, the server, whatever tie that instance of that object to that user? If all of these objects float around on the server till I destroy them won't there be lots of memory used by objects?

正如我在一开始所说的那样,这可能是一个非常基本的基本问题,但是我还没有得到尤里卡的支持,我可能会回到更简单的PHP.

As I said at the start it's probably a really basic, fundatmental question but I'm yet to have my eureka moment with this, I might go back to simpler PHP.

推荐答案

默认情况下,会话数据(即$ _SESSION中的所有数据)已序列化并存储到请求之间的文件中.调用session_start()时,数据会自动反序列化.

Session data (that is all the data in $_SESSION) is by default serialized and stored to file between requests. The data gets unserialized automatically when session_start() is called.

摘录自有关会话处理的PHP手册(重点是我的) :

From the PHP manual on Session Handling (emphasis mine):

会话支持允许您注册任意数量的变量,这些变量将在请求之间保留.当访问者访问您的站点时,PHP将自动检查(如果session.auto_start设置为1)或根据您的请求(显式地通过session_start()或隐式地通过session_register())检查是否已随请求发送特定的会话ID.在这种情况下,将重新创建先前保存的环境.

The session support allows you to register arbitrary numbers of variables to be preserved across requests. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

两次请求之间的任何内容都不会保留在内存中. PHP具有无共享的体系结构,这意味着除非您使用专用的缓存机制,否则所有对象都将在每次请求时重新创建.

Nothing is persisted in memory between requests. PHP has a shared nothing architecture, meaning all objects are recreated on each request anew, unless you are using dedicated cache mechanisms.

这篇关于PHP如何跟踪创建的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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