PHP Multiple Calls to Server Share Objects?

Posted by user1513171 on Stack Overflow See other posts from Stack Overflow or by user1513171
Published on 2014-06-07T03:16:03Z Indexed on 2014/06/07 3:25 UTC
Read the original article Hit count: 397

Filed under:
|
|
|
|

I’m wondering this about PHP on Apache. Do multiple calls to the server from different users—could be sitting next to each other, in different states, different countries, etc…—share memory?

For example, if I create a static variable in a PHP script and set it to 1 by default, then user1 comes in and it changes to 2, and then almost at the exactly same time, user2 comes in, does he see that static variable with a value of 1 or 2?

An even better example is this class I have in PHP:

class ApplicationRegistry {
private static $instance;

private static $PDO;

private function __construct() {
    self::$PDO = $db = new \PDO('mysql:unix_socket=/........');
    self::$PDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}

static function instance() {
    if(!isset(self::$instance)) {
        self::$instance = new self();
    }
    return self::$instance;
}

static function getDSN() {
    if(!isset(self::$PDO)) {
        self::instance();
        return self::$PDO;
    }
    return self::$PDO;
}
} 

So this is a Singleton that has a static PDO instance. If user1 and user2 are hitting the server at the exact same time are they using different instances of PDO or are they using the same one?

This is a confusing concept for me and I'm trying to think of how my application will scale.

© Stack Overflow or respective owner

Related posts about php

Related posts about apache