I was playing around with these functions and made a class in the process. This will of course be slower than accessing a variable locally, but gives the ability to store variables in a shared environment and gives many running scripts the understanding that it should access them from the shared area. This should also auto destroy the shared memory area once no more scripts have a link to the data (when all scripts use this class).
<?php
class SharedMemory{
private $nameToKey = array();
private $key;
private $id;
function __construct($key = null){
if($key === null){
$tmp = tempnam('/tmp', 'PHP');
$this->key = ftok($tmp, 'a');
$this->id = shm_attach($this->key);
$this->nameToKey[] = '';
$this->nameToKey[] = '';
$this->updateMemoryVarList();
shm_put_var($this->id, 1, 1);
}else{
$this->key = $key;
$this->id = sem_get($this->key);
$this->refreshMemoryVarList();
shm_put_var($this->id, 1, shm_get_var($this->id, 1) + 1);
}
if(!$this->id)
die('Unable to create shared memory segment');
}
function __sleep(){
shm_detach($this->id);
}
function __destruct(){
if(shm_get_var($this->id, 1) == 1){
$this->remove();
}else{
shm_detach($this->id);
shm_put_var($this->id, 1, shm_get_var($this->id, 1) - 1);
}
}
function __wakeup(){
$this->id = sem_get($this->key);
shm_attach($this->id);
$this->refreshMemoryVarList();
shm_put_var($this->id, 1, shm_get_var($this->id, 1) + 1);
}
function getKey(){
return $this->key;
}
function remove(){
shm_remove($this->id);
}
function refreshMemoryVarList(){
$this->nameToKey = shm_get_var($this->id, 0);
}
function updateMemoryVarList(){
shm_put_var($this->id, 0, $this->nameToKey);
}
function __get($var){
if(!in_array($var, $this->nameToKey)){
$this->refreshMemoryVarList();
}
return shm_get_var($this->id, array_search($var, $this->nameToKey));
}
function __set($var, $val){
if(!in_array($var, $this->nameToKey)){
$this->refreshMemoryVarList();
$this->nameToKey[] = $var;
$this->updateMemoryVarList();
}
shm_put_var($this->id, array_search($var, $this->nameToKey), $val);
}
}
$sharedMem = new SharedMemory();
$pid = pcntl_fork();
if($pid){
sleep(1);
echo "Parent Says: " . $sharedMem->a . "\n";
echo "Parent Changed to 0\n";
$sharedMem->a = 0;
echo "Parent Says: " . $sharedMem->a . "\n";
sleep(2);
echo "Parent Says: " . $sharedMem->a . "\n";
}else{
$sharedMem->a = 2;
echo "Child Changed to 2\n";
echo "Child Says: " . $sharedMem->a . "\n";
sleep(2);
echo "Child Says: " . $sharedMem->a . "\n";
echo "Child Added 1\n";
$sharedMem->a++;
echo "Child Says: " . $sharedMem->a . "\n";
}
?>