Confirmation That PHP Static Variables Do Not Persist Across Requests

Does static variables in php persist across the requests?

No, while a static variable will stay for the current request you'll need to add it to a session to persist it's value across requests.

Example:

session_start();

class Car {
public static $make;
public function __construct($make) {
self::$make = $make;
}
}

$c = new Car('Bugatti');
echo '<p>' . Car::$make . '</p>';
unset($c);

if (!isset($_SESSION['make'])) {
echo '<p>' . Car::$make . '</p>';
$c = new Car('Ferrari');
echo '<p>' . Car::$make . '</p>';
}

$_SESSION['make'] = Car::$make;

echo '<p>' . $_SESSION['make'] . '</p>';

PHP static variables across multiple .php pages

Static data is only static within the context of a class, meaning a static data member in a class is shared by all instances of that class.

What you seem to be talking about is data persisting across multiple HTTP requests. Static data won't do that for you. That's what $_SESSION data is for.

To put it another way: once a script finishes servicing the current request, it completely dies. All data is had is completely cleaned up. The new request starts fresh.

Session data persists until PHP decides to clean it up or you manually destroy it. Typically all you have to do to use session data is put in your script:

Script 1: mailbox.php

session_start();
$_SESSION['mailbox'] = array( /* messages */ );

Script 2: showmail.php

session_start();
$mailbox = $_SESSION['mailbox'];

One thing to note: if your script is long-running try and put a session_commit() in as soon as possible because session access blocks in PHP, meaning if another script tries to session_start() for the same user it will block until the first script finishes executing or releases the session.

How to handle php static class variables inside functions

You're not missing any OOP principle, you're missing the standard implementation of PHP processes. Normally, web servers are designed to have php scripts execute then die. There is no persistence in memory when you visit two different pages.

Can you implement PHP to continue serving with the same memory? Surely could, but no web server implementation I know of works this way.

Most people use sessions (server side) or cookies (client side) to keep persistent data across multiple requests.

How long do PHP static variables persist?

PHP variables persist for the lifetime of the script running through the interpreter. In the case of a web request, this is the lifetime of handling the requests. Your three cases are all requests to a server, and thus are handled the same: the static variables survive until the script terminates after handling the request.

The life span of PHP (and its variables) over a request:

  1. Request is sent to server, whether by user, ajax, curl through PHP or what-have-you
  2. Relevant PHP script is executed, whether as a module on your web server, a CGI worker process, or other options
  3. Script is executed, response to the request (if any) is created and sent
  4. (optional) script continues to execute some other job until eventual termination, at which time all its variables die with it.

Can two parallel invocations of a static method exists in PHP?

See Confirmation that PHP static variables do not persist across requests for the information you need.

Standard PHP does not do any locking or waiting. It will just run the method and the 2 seperate request do not known of each other and they can't access each others memory(In the given case).

This is a race condition. It depends which request gets processed first. A http request is handled by a webserver and the webserver spawns a php process to process the php script.

Zend Global Variable in an Extension Persisting Across Multiple Requests

The only way is to use some form of Inter-process communication. For instance, you could use shared memory (but you'll have to synchronize the access to it).

Remember the most common way to run PHP is without thread support – multiple processes, one process serving only one client at a time. The reason is that PHP is substantially faster in this configuration.

PHP static variables memory usage

and should that even be really considered?

No you shouldn't worry about statics for that reason.

The reason you have to worry about the use of static is the fact that you cannot unit test your code anymore and you have tightly coupled classes and code to Something::DB (i.e. the Something class) and you are working with global state.

Also check out an previous answer by me about how to handle those "global" instances: Which is the best practice to access config inside a function?

PHP Static variable not working as expected

I believe you misunderstand what static vars do. Try this code and you may understand better:

echo getNextQuestionID() . ", " getNextQuestionID() . ", " getNextQuestionID();

And you will see what I mean.

The static var only lives as long as the script does.

The reason it is returning 0 on the first run instead of 1 is because you are using the postfix operator $var++ instead of the prefix version - ++$var. The difference is is that the increment only gets applied when using the postfix operator after the function returns - but if you use the prefix operator it is applied before the function returns.



Related Topics



Leave a reply



Submit