GlobalData Variable Sharing Component
(Requires Workerman version >= 3.3.0)
Source code available at: https://github.com/walkor/GlobalData
Note
GlobalData requires Workerman version >= 3.3.0
Installation
composer require workerman/globaldata
Principle
By utilizing PHP's __set __get __isset __unset magic methods, communication with the GlobalData server is triggered, and the actual variable is stored on the GlobalData server. For example, when setting a non-existent property for the client class, the __set magic method is triggered. In the __set method of the client class, a request is sent to the GlobalData server to store a variable. When accessing a non-existent variable in the client class, the class's __get method is triggered, and the client will request the GlobalData server to read this value, thus completing inter-process variable sharing.
require_once __DIR__ . '/vendor/autoload.php';
// Connect to the Global Data server
$global = new GlobalData\Client('127.0.0.1:2207');
// Trigger $global->__isset('somedata') to query the server if it has stored a value with key somedata
isset($global->somedata);
// Trigger $global->__set('somedata',array(1,2,3)), notifying the server to store the value of somedata as array(1,2,3)
$global->somedata = array(1,2,3);
// Trigger $global->__get('somedata') to query the server for the value corresponding to somedata
var_export($global->somedata);
// Trigger $global->__unset('somedata'), notifying the server to delete somedata and its corresponding value
unset($global->somedata);