Search code examples
phpapachepdostaticscale

PHP Multiple Calls to Server Share Objects?


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.


Solution

  • Do multiple calls to the server from different users (could be sitting next to each other, in different states, different countries, etc) share memory?

    No. Each time a PHP script is called by Apache a new instance of the script is run.

    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?

    Both users will see 1 and will only see 2 if they interact with your script in a way that will change that to 2. All actions are indpenedent.

    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?

    They can hit it so perfectly at the same time your mind would be blown, but each server request spawns a new Apache process which in turn launches a new PHP process which then runs your code.

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

    You are mixing up two different concepts: Scalability and the PHP code state when loaded. This is the kind of stuff you do have to worry about intensely with Ruby & Java programs since they typically are always running when they are on in a server.

    But PHP only runs when an Apache request is made. And that’s it. And scalability is something you should not worry too much about right now. Just code & create an application you like. Soon enough you will find choke points that appear when the code is run.

    UPDATE: And since you mention in comments you are worried about IDs being in conflict since you are not using MySQL’s auto increment capabilities, that is a problem. That could potentially get you in trouble, but hard to say without looking at actual code.

    The issue in this example you are making with MySQL has nothing to do with PHP but rather your insistence at not auto incrementing id values. That is a strength of MySQL & it is best you refactor your code to put the burden of id creation on the MySQL server itself.

    If you are truly worried about this & absolutely cannot set auto increment to your MySQL database for some reason, consider using a randomly generated MD5 hash or something similar instead of an id. Or perhaps in conjunction with a normal numerical ID. That way the odds of 2 users hitting the server at the exact same moment & getting the same ID are extremely low with a random hash.