shm_get_var

(PHP 4, PHP 5, PHP 7, PHP 8)

shm_get_varDevuelve una variable desde la memoria compartida

Descripción

shm_get_var(resource $shm_identifier, int $variable_key): mixed

shm_get_var() devuelve la variable con la clave dada por variable_key, del segmento de memoria compartida dado. La variable todavía está presente en la memoria compartida.

Parámetros

shm_identifier

Segmento de memoria compartida, obtenido desde shm_attach().

variable_key

La clave de la variable.

Valores devueltos

Devuelve la variable con la clave dada.

Ver también

  • shm_has_var() - Verificar si existe una entrada específica
  • shm_put_var() - Inserta o actualiza una variable en la memoria compartida

add a note

User Contributed Notes 6 notes

up
2
ricardophp at yahoo dot com dot br
16 years ago
A fully functional sample ...

<?php
echo "<PRE>\n";

define("FOPEN_RESOURCE", 1);

$shm_id = shm_attach(FOPEN_RESOURCE);
if (
$shm_id === false) {
exit(
"Fail to attach shared memory.\n");
}

$fopen_resource = fopen("/tmp/phpSharedMemory.bin", "w");

$a = array("Teste1", 1);
if (!
shm_put_var($shm_id, $a, $a)) {
exit(
"Failed to put var 1 in shared memory $shm_id.\n");
}

echo
"F: ".$a[0].":".$a[1]."\n";
$pid = pcntl_fork();

if(
$pid == -1) {
die(
"could not fork\n");
}
else if (
$pid) {
$a = array("Teste2", 3);
if (!
shm_put_var($shm_id, $a, $a)) {
exit(
"Failed to put var 1 in shared memory $shm_id.\n");
}
echo
"P1: ".$a[0].":".$a[1]."\n";
} else {
sleep(2);
$a = shm_get_var($shm_id, $a);
echo
"P2: ".$a[0].":".$a[1]."\n";
}
pcntl_wait($status);
exit();
?>
up
1
JM
19 years ago
To follow up on the posts by anonymous, Bob Van Zant and chris at free-source dot com below (or, as must people inexplicably write, above) regarding the PHP warning and FALSE that shm_get_var returns if the variable key doesn't exist:

My tests (with PHP4.3.4) show that defined() is useless here. Because the function defined(string) checks whether the constant whose name is string exists, the code
<?php
if ( defined(@shm_get_var($mutex, $mutex_key)) {
...
}
?>
acts the same ("..." does not get executed) whether the variable is defined or not--unless $mutex_key happens to identify a valid string that happens to be the name of a constant. :)

Rather,
<?php
if ( @shm_get_var($mutex, $mutex_key) === FALSE ) {
...
}
?>
works, provided the object that was stored isn't actually FALSE (via <?php shm_put_var($mutex, $mutex_key, FALSE); ?>)

It would be nice to have a completely air-tight solution, though. D'oh!
up
0
hackmac at nowhere dot com
15 years ago
hello everyone i came up with some sort of solution to the shm_get_var()
returns false on error/returns a boolean false variable problem.

test script

<?php

error_reporting
(E_ALL);
ini_set('display_errors', '1');

echo
'<pre>';
echo
ini_get('sysvshm.init_mem');

require_once(
'ClassShmWrapper.php5');

$nKey = ftok(__FILE__,'x');

$myShm = new ClassShmWrapper($nKey);

$myShm->attachToSegment();

#$mValue = range(1,rand(3,10));
#$myShm->nVarKey = count($mrValue);

#$mValue = FALSE;

/*
$mValue = TRUE;
$myShm->nVarKey = 1;
$myShm->mVar = $mValue;
$myShm->putVarToSegment();
*/

#$myShm->nVarKey = 2;
$myShm->nVarKey = 1;

if (
$myShm->getVarFromSegment()) {
echo
"found var in shm\n";
}
else {
echo
"could NOT find var in shm\n";
}

$myShm->detachFromSegment();

echo
"\ndumping " . '$myShm->mVar' . "\n";
var_dump($myShm->mVar);

?>

class for using the shm_ functions & class for storing boolean values

<?php

class ClassShmWrapper {

public
$nPermissions;
public
$nKey;
public
$nBytesMemorySize;

public
$nShmId;

public
$nVarKey;
public
$mVar;



public function
__construct($nKey,$nBytesMemorySize=50000,$nPermissions=0666) {

$this->nKey = $nKey;
$this->nBytesMemorySize = $nBytesMemorySize;
$this->nPermissions = $nPermissions;
}


public function
attachToSegment() {
$this->nShmId = shm_attach($this->nKey,$this->nBytesMemorySize,$this->nPermissions);
}


public function
detachFromSegment() {
shm_detach($this->nShmId);
}


public function
removeSegment() {
shm_remove($this->nShmId);
}


public function
getVarFromSegment() {

$mVar = NULL;

if ((
$mVar = @shm_get_var($this->nShmId,$this->nVarKey)) !== FALSE) {

$this->mVar = $mVar;

unset(
$mVar);

/*
For variables of type boolean we need to access an object property which stores the boolean value.
This is needed as shm_get_var() could return FALSE when returning a boolean variable set to FALSE
or when a non-existing variable key was tried to access!
*/

if ($this->mVar instanceof ClassShmBooleanWrapper) {
$this->mVar = $this->mVar->bVal;
}

return
TRUE;
}
else {

return
FALSE;
}

}


/**
* Puts a PHP variable into shared memory (or updates an existing one for the given variable key).
*
* @return boolean returns TRUE on success/FALSE on error
*/
public function putVarToSegment() {

// cmp -> comment getVarFromSegment()
if (is_bool($this->mVar)) {

return
shm_put_var($this->nShmId,$this->nVarKey,new ClassShmBooleanWrapper($this->mVar));

}
else {
return
shm_put_var($this->nShmId,$this->nVarKey,$this->mVar);
}

}


public function
removeVarFromSgement() {

shm_remove_var($this->nShmId,$this->nVarKey);
}

}
// end class


class ClassShmBooleanWrapper {

public
$bVal;

public function
__construct($bVal) {
$this->bVal = $bVal;
}

}
// end class

?>
up
-1
Anonymous
19 years ago
You will still receive a notice use @:

if(!defined(@shm_get_var($mutex, $mutex_key))) {
shm_put_var($mutex, $mutex_key, 0);
}
up
-1
Bob Van Zant
19 years ago
This seems to work fine to detect the lack of presence of a key in shared memory and then init it to 0 when found:

if(!defined(shm_get_var($mutex, $mutex_key))) {
shm_put_var($mutex, $mutex_key, 0);
}
up
-5
serg at red-sign dot ru
11 years ago
If any body has problem with shm_get_var();

this code correctly work with boolean-values^
<?php
if(shm_has_var ($this->shm_mem_id, $_varno)){
$_value = shm_get_var($this->shm_mem_id, $_varno);
}else{
$_value = NULL;
}
?>
To Top