PHPackages                             ninsuo/php-shared-memory - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ninsuo/php-shared-memory

ActiveLibrary

ninsuo/php-shared-memory
========================

Share variables across multiple PHP apps

v2.1.0(2y ago)4021.3k—6.7%5[1 issues](https://github.com/ninsuo/php-shared-memory/issues)2MITPHPPHP &gt;=5.3.3

Since Oct 18Pushed 2y ago3 watchersCompare

[ Source](https://github.com/ninsuo/php-shared-memory)[ Packagist](https://packagist.org/packages/ninsuo/php-shared-memory)[ Docs](https://github.com/ninsuo/php-shared-memory)[ RSS](/packages/ninsuo-php-shared-memory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)DependenciesVersions (3)Used By (2)

php-shared-memory
=================

[](#php-shared-memory)

Share variables across multiple PHP apps

This class works like `\stdClass`, but one instance of SharedMemory can be used simultaneously in several PHP applications.

Because this class stores and restores its data every time a property is requested or set, data are always fresh between your applications. And because PHP has a great built-in advisory lock feature, there could be as many applications as you want, there is no concurrent access to the synchronization file.

Use cases
---------

[](#use-cases)

Long-running tasks : when there is a long-running task run in background from a web application, this is diffcult to display progression information. With SharedMemory, just set $shared-&gt;progress = x in your task, and echo $shared-&gt;progress in your web app.

Multi task : there is no built-in threads functions in PHP, so if we need to simulate threads, we execute several PHP tasks (forks, execs, ...), and keep control on resources and results. But from here, there is no way for all children processes to communicate each other. SharedMemory gives you a centralized data pool, where every processes can put about anything.

Installation
------------

[](#installation)

If you want a standalone file to manage your shared memory, you can look at the `v1.5.0` release:

If you're building a real-life project, you'd better use Composer:

### Install Composer

[](#install-composer)

If you have curl, you can use:

`curl -sS https://getcomposer.org/installer | php`

Else, you can use the PHP method instead:

`php -r "readfile('https://getcomposer.org/installer');" | php`

#### Add the following to your `composer.json`:

[](#add-the-following-to-your-composerjson)

```
{
    "require": {
        "ninsuo/php-shared-memory": "dev-master"
    }
}
```

#### Update

[](#update)

`php composer.phar update`

Usage
-----

[](#usage)

This class works the same way as `stdClass`, but you should give a storage in its constructor. This storage will be used to store and retrieve your data: use the same storage on several apps to get the same instance of a variable.

```
require("vendor/autoload.php");

use Fuz\Component\SharedMemory\Storage\StorageFile;
use Fuz\Component\SharedMemory\SharedMemory;

// On both apps
$storage = new StorageFile('/tmp/demo.sync');

// First app
$sharedA = new SharedMemory($storage);
$sharedA->foo = 'bar';

// Second app
$sharedB = new SharedMemory($storage);
echo $sharedB->foo; // bar
```

Or a complete working example:

```
