Search code examples
php-extensionphp-internals

Make ZVAL persistent across the SAPI?


A ZVAL is typically created with emalloc so it is destroyed at the end of a page request. Is there a way to take an existing ZVAL and make it persist in the SAPI (equivalent of pemalloc)? What about creating a ZVAL with pemalloc?

Ideally what I'd like to do (in PHP code) is this:

class Object
{
    public $foo;
}

if(!($object = persist("object")))
{
   $object = persist("object", new Object());
}

$object->foo[] = "bar";

print count($object->foo);

On each request count would return +1 (assuming the same PHP "worker" is used every time - I'm using PHP-FPM).


Solution

  • You're basically describing http://lxr.php.net/opengrok/xref/PHP_5_3/ext/sysvshm/sysvshm.c#242

    The closest you can get without duplicating functionality which is already in shm is https://github.com/flavius/php-persist. The catch: in a prefork/multiprocess SAPI (like apache's), different requests from the same client may end up in different processes, and as such, you'll see different data (try it on Linux + Firefox with a hard refresh every time in the browser).

    Note: It's a work-in-progress, currently it persists only an integer. Adding an array should be trivial. Patches welcome. It still needs the deserialization part, and to actually use persist()'s first parameter.