关于 PHP 范围的问题 - 从 Java 程序员的角度来看 [英] Questions about scope in PHP - from a Java Programmer's Perspective
问题描述
我对 PHP 还是很陌生,所以我正在尝试理解 PHP Web 应用程序中的作用域概念.
I'm still fairly new to PHP and so I'm attempting to understand scope concepts within PHP web applications.
在 Java 世界中,Java Web 应用程序 - 使用 Java Server Pages (JSP) 及更高版本 - 将允许 Java Bean 具有以下级别的范围:
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:
- 页面
- 请求
- 会话
- 申请
尝试将这些映射到 PHP 的范围功能:
Attempting to map these to PHP's scoping capabilities:
- 页面:不是真的,而是调用本地的对象在函数调用后被认为消失了",所以它有点像页面范围
- 请求:通过使用$_REQUEST 超级全局(不确定这会去哪里...... Cookies?隐藏字段?URL 参数?)进行"
- 会话:使用 PHP 的 $_SESSION 超级全局(其中一些文档和论坛反馈指出,出于安全原因,这不是放置敏感信息的好地方)
- 应用:使用 PHP 的 APC(堆栈溢出链接)
我是完全出去吃午饭了还是这些相当相似?我知道一个主要区别是 PHP 的 ["Shared Nothing"][5] 架构与 Java 的架构相比,后者允许共享.
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.
推荐答案
您走对了.PHP 确实是 Share-Nothing.
You're on the right track. PHP is indeed Share-Nothing.
在 Web 上下文中,一个 php 应用程序总体上为每个 HTTP 请求运行一次.这意味着对于每个 HTTP 请求,解释器都会读取、解析和执行脚本(这是简化的 - 使用像 APC 这样的操作码缓存消除了读取/解析开销).
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 以 superglobals 的形式向脚本提供输入,例如 $_REQUEST 和 $_SESSION.超全局变量与常规全局变量的不同之处在于它们在每个范围内自动可用,因此无需使用 global
关键字.
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.
任何在请求之间持续存在的数据都需要存储在外部.要跨请求共享数据以维护用户的状态,您通常使用 $_SESSION,默认情况下它被序列化并写入磁盘上的文件(但可以配置为使用内存缓存或数据库).要在会话之间共享的数据(我认为这类似于 JSP 世界中的应用程序范围)需要存储在外部某处.您可以使用 APC 或 memcache 之类的内存缓存,或者将平面文件写入磁盘,或者将内容粘贴到数据库中,或者使用您能想到的任何其他方案.归根结底,没有任何内置的东西.
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.
要在非全局范围内(即:在函数内部)引用全局变量,您需要使用global
关键字将符号导入局部范围.PHP 以这种方式工作以使其更难意外地破坏全局变量.
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.
这篇关于关于 PHP 范围的问题 - 从 Java 程序员的角度来看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!