关于 PHP 范围的问题 - 从 Java 程序员的角度来看
我对 PHP 还很陌生,所以我试图理解 PHP Web 应用程序中的范围概念。
在 Java 世界中,Java Web 应用程序(使用 Java Server Pages (JSP) 及以上)将允许 Java Bean 具有以下范围级别:
- 页面
- 请求
- 会话
- 应用程序
尝试将这些映射到 PHP 的范围功能:
- 页面:不是真的,但是调用本地的对象在函数调用后被视为“消失”,因此它有点像页面范围
- 请求:通过使用“$_REQUEST 超级全局”进行(不确定它去哪里...Cookie?隐藏字段? URL 参数?)
- 会话:使用 PHP 的 $_SESSION 超级全局(其中一些文档和论坛反馈表明,出于安全原因,这不是放置敏感信息的好地方)
- 应用程序:使用 PHP 的 APC(堆栈溢出链接)
Am我完全出去吃午饭了,还是这些相当相似?我知道 PHP 的一个主要区别是 PHP 的 [“Shared Nothing”][5] 架构与 Java 的架构相比,它允许共享
。
I'm still fairly new to PHP and so I'm attempting to understand scope concepts within PHP web applications.
In the Java world a Java web app - using Java Server Pages (JSP) and upward - will allow a Java Bean to have the following levels of scope:
- Page
- Request
- Session
- Application
Attempting to map these to PHP's scoping capabilities:
- Page: not really but objects that are local to a call are considered 'gone' after the function call is made so it's sort of like a page scope
- Request: made by using the "$_REQUEST super global (not sure where this goes ... Cookies? Hidden fields? URL parameters?)
- Session: using PHP's $_SESSION super global (where some of the documentation and forum feedback states that this is not a great place to put sensitive information for security reasons)
- Application: using PHP's APC (a Stack Overflow link)
Am I totally out to lunch or are these reasonably similar? I know that one major difference is PHP's ["Shared Nothing"][5] architecture as compared to Java's which is to allow for sharing.
Any advice/guidance/sobering corrections most welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你走在正确的轨道上。 PHP 确实是不共享的。
在 Web 上下文中,PHP 应用程序针对每个 HTTP 请求完整运行一次。这意味着对于每个 HTTP 请求,解释器都会读取、解析并执行脚本(这是简化的 - 使用像 APC 这样的操作码缓存可以消除读取/解析开销)。
PHP 以 superglobals 的形式向脚本提供输入,例如$_REQUEST 和 $_SESSION。超全局变量与常规全局变量不同,因为它们在每个作用域中自动可用,因此无需使用
global
关键字。请求之间持续存在的任何数据都需要存储在外部。要跨请求共享数据以维护用户的状态,通常使用 $_SESSION,它默认被序列化并写入磁盘上的文件(但可以配置为使用内存缓存或数据库)。要在会话之间共享的数据(我认为这类似于 JSP 世界中的应用程序范围)需要存储在外部某个地方。您可以使用 APC 或 memcache 等内存缓存,或者将平面文件写入磁盘,或者将内容保存在数据库中,或者使用您能想到的任何其他方案。归根结底,没有任何内置的东西。
除了超全局变量之外,变量作用域相当无聊。默认情况下,变量存在于创建它们的范围内。
要在非全局作用域(即:函数内部)引用全局变量,您需要使用
global
关键字将符号导入到局部作用域中。 PHP 以这种方式工作可以使全局变量更难被意外破坏。这些内容以及更多内容手册中都有详细介绍。
You're on the right track. PHP is indeed Share-Nothing.
In a web context, a php application runs, in it's entirety, once for each HTTP request. This means for every HTTP request the interpreter reads, parses and executes the script (this is simplified - using an opcode cache like APC removes the reading/parsing overhead).
PHP feeds input to the script in the form of superglobals, such as $_REQUEST and $_SESSION. Superglobals are different from regular global variables in that they're automatically available in every scope, so there's no need to use the
global
keyword.Any data that persist between requests need to be stored externally. To share data across requests to maintain state for a user, you typically use $_SESSION, which by default is serialized and written to files on disk (but can be configured to use a memory cache or database). Data that are to be shared between sessions (which I suppose is similar to the Application scope in the JSP world) need to be stashed somewhere external. You can use a memory cache like APC or memcache, or write flat files to disk, or stick stuff in a database, or use any other scheme you can come up with. At the end of the day, there's nothing built-in.
Aside from superglobals, variable scope is fairly boring. By default, variables live in the scope in which they're created.
To reference a global variable in a non-global scope (ie: inside a function), you need to import the symbol into the local scope using the
global
keyword. PHP works this way to make it harder to accidentally clobber global variables.This stuff, and more, is covered pretty well in the manual.
您可能应该看看这个:
http://php.net/manual/en/ language.variables.scope.php
你有本地和全局作用域、超全局变量、静态变量。该页面解释了它们各自的工作原理。
You should probably look at this:
http://php.net/manual/en/language.variables.scope.php
You've got local and global scope, superglobals, static variables. And that page explains how those each work.