GlobalData Component Client

(Requires Workerman version >= 3.3.0)


void \GlobalData\Client::__construct(mixed $server_address)

Instantiate a \GlobalData\Client client object. Share data between processes by assigning properties on the client object.


GlobalData server server address in the format <ip address>:<port>, for example

If it is a GlobalData server cluster, pass in an array of addresses, for example array('', '')


Supports assignment, reading, isset, and unset operations.
Also supports CAS atomic operations.


use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

// GlobalData Server
$global_worker = new GlobalData\Server('', 2207);

$worker = new Worker('tcp://');
// When the process starts
$worker->onWorkerStart = function()
    // Initialize a global global data client
    global $global;
    $global = new \GlobalData\Client('');
// When the server receives a message
$worker->onMessage = function(TcpConnection $connection, $data)
    // Change the value of $global->somedata, and other processes will share this $global->somedata variable
    global $global;
    echo "now global->somedata=".var_export($global->somedata, true)."\n";
    echo "set \$global->somedata=$data";
    $global->somedata = $data;

All usage (can also be used in php-fpm environment)

require_once __DIR__ . '/vendor/autoload.php';

$global = new Client('');


$global->abc = array(1,2,3);



var_export($global->add('abc', 10));

var_export($global->increment('abc', 2));

var_export($global->cas('abc', 12, 18));


The GlobalData component cannot share data of resource types, such as mysql connections, socket connections, etc.

If using GlobalData/Client in the Workerman environment, please instantiate the GlobalData/Client object in the onXXX callback, such as instantiating it in onWorkerStart.

Cannot operate shared variables like this.

$global->somekey = array();

$global->someObject = new someClass();
$global->someObject->someVar = 'xxx';

It can be done like this

$somekey = array();
$somekey[] = 'xxx';
$global->somekey = $somekey;

$someObject = new someClass();
$someObject->someVar = 'xxx';
$global->someObject = $someObject;