PHP Scope, $GLOBALS & Singletons
I'm having a difficult time getting answers to a few very basic PHP questions. The video tutorials I've watched are all for the basics of server-side scripting using PHP, but there's very little actually explaining how PHP works under the hood.
Say we have 2 users connecting to t开发者_Go百科he same LAMP server from different parts of the world at the same time, and are both requesting Widget.php.
(a) How does Apache/PHP effectively "copy 2 instances" of the Widget.php script & its dependencies so that the 2 HTTP requests are handled separately?
(b) Stemming from part (a) above, how does PHP compartmentalize user requests to keep them separate? If Widget.php uses a session var called $_SESSION['cheese']
, how does PHP keep cheese's value separate for both of the 2 users?
(c) Are PHP $GLOBALS
user-wide or application-wide? Meaning, for global var $GLOBALS['bread']
, do user 1 and 2 both share the same reference to it, or does PHP keep them separate as it did with the session variable above in (b)?
(d) What's the difference between a PHP $GLOBALS
variable, and an object that implements a singleton design? If application only ever has access to 1 instance of the singleton, isn't it a global? Are there performance considerations that should be taken into account?
Think of a php application as a script that is executed everytime the server requests it rather than as an application that exists in the server's memory. When an HTTP request comes in the script is run therefore $_SESSION['cheese'] will be unique to the user who requests it as the session id is stored client side in a browser cookie.
Globals are simply variables with script wide scope so it will be the same for the user who sent the HTTP request script wide. A singleton is a class which can only have one instance and is not the same as a global.
PHP scripts are stateless in that when the script has finished executing all values that objects hold will be lost. The only way to get an object to persist between executions is to serialise to a database or to the session or filesystem.
(a) PHP operates like most other CGI implementations. When the webserver receives a request for a specific URL that happens to be a .cgi/.php script, then the interpreter is invoked with the referenced script. The PHP interpreter runs it, and sends the output back to the webserver/client. After it's finished, that CGI interpreter terminates and takes all its runtime data with it.
That explains why variables do not persist or are shared between two distinct PHP scripts or invocations. It works the same for the ordinary mod_php handler. Only there it's simple process forking (duplication) and termination.
(b) The $_SESSION array is handled by PHP. It can discern the per-user storage using the unique cookie key. And since each CGI or mod_php process does have a separate variable pool, there is really no issue. PHP just needs a bit of file locking to prevent two scripts from overwriting the session store when the same user requested two scripts at once.
(c) "Global" variables also only exist per-process. They are gone like everything else when the PHP script has finished.
(d) That's two different concepts. I see what you mean with the usage similarities. But globals are just a shared scope, more closely circumscribed as value object maybe. You can access both using a global identifier, but that's about the similarities.
a) you're asking how apache/php works?? =P that's ambitious... long story short: php will compile the Widget.php on every request, and apache will send the response on Http protocol
b) your server will save the SESSION data in a database or file or magical place (dont really know where and how it does it, all i care is that it's saved). On each request, the browser will send a session cookie containing the session id that PHP will use to load the appropriate info into the $_SESSION variable. (like a primary key on a database table)
c) it's user-wide (script-side), but it will not be preserved between pages like the session.
d) $GLOBALS is a variable, a singleton is an object that has attributes, methods,accessors, can be inherited, overwritten, etc
Good Luck!
for the B, the user hold a unique ID which is called UUID, it's stored in cookies. so the server can distinguish users by reading this unique ID-s and returning appropriate value of session.
$GLOBALS are Application wide.
Singleton objects are somehow globals but you don't necessarily have an instance of one, existing at concrete moment...
精彩评论