2024 年 PHP Conference Japan

shm_get_var

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

shm_get_var從共享記憶體返回一個變數

說明

shm_get_var(SysvSharedMemory $shm, int $key): mixed

shm_get_var() 返回指定共享記憶體區段中,給定 key 的變數。該變數仍然存在於共享記憶體中。

參數

shm

shm_attach() 獲得的共享記憶體區段。

鍵值 (key)

變數的鍵值。

回傳值

回傳指定鍵值所對應的變數。

更新日誌

版本 說明
8.0.0 shm 現在需要一個 SysvSharedMemory 實例;先前需要的是 資源

參見

新增筆記

使用者貢獻的筆記 6 則筆記

ricardophp at yahoo dot com dot br
16 年前
一個完整的功能範例…

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

define("FOPEN_RESOURCE", 1);

$shm_id = shm_attach(FOPEN_RESOURCE);
if (
$shm_id === false) {
exit(
"無法附加共享記憶體。\n");
}

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

$a = array("Teste1", 1);
if (!
shm_put_var($shm_id, $a, $a)) {
exit(
"無法將變數 1 放入共享記憶體 $shm_id。\n");
}

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

if(
$pid == -1) {
die(
"無法 fork\n");
}
else if (
$pid) {
$a = array("Teste2", 3);
if (!
shm_put_var($shm_id, $a, $a)) {
exit(
"無法將變數 1 放入共享記憶體 $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();
?>
JM
19 年前
接續 anonymous、Bob Van Zant 和 free-source.com 的 chris (或者,如同大部分人莫名其妙地寫的,在上面) 關於 shm_get_var 如果變數鍵值不存在時回傳 PHP 警告和 FALSE 的文章

我的測試 (使用 PHP4.3.4) 顯示 defined() 在這裡沒有用處。因為函數 defined(string) 檢查名稱為 string 的常數是否存在,程式碼
<?php
if ( defined(@shm_get_var($mutex, $mutex_key)) {
...
}
?>
無論變數是否已定義,其作用都相同(「...」不會被執行)——除非 $mutex_key 恰好標識一個有效的字串,而該字串恰好是常數的名稱。 :)

相反地,
<?php
if ( @shm_get_var($mutex, $mutex_key) === FALSE ) {
...
}
?>
可以正常運作,前提是儲存的物件實際上不是 FALSE(透過 <?php shm_put_var($mutex, $mutex_key, FALSE); ?>

不過,如果能有一個完全嚴密的解決方案就更好了。唉!
hackmac at nowhere dot com
15 年前
大家好,我想出了 shm_get_var() 的一種解決方案
在錯誤時返回 false / 返回布林值 false 變數的問題。

測試腳本

<?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
"在共享記憶體中找到變數\n";
}
else {
echo
"在共享記憶體中找不到變數\n";
}

$myShm->detachFromSegment();

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

?>

用於 shm_ 函數的類別以及用於儲存布林值的類別

<?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

?>
匿名
19 年前
您仍然會收到通知,請使用 @

if(!defined(@shm_get_var($mutex, $mutex_key))) {
shm_put_var($mutex, $mutex_key, 0);
}
Bob Van Zant
19 年前
這似乎可以正常運作,用於偵測共享記憶體中缺少金鑰,並在找到時將其初始化為 0

if(!defined(shm_get_var($mutex, $mutex_key))) {
shm_put_var($mutex, $mutex_key, 0);
}
serg at red-sign dot ru
11 年前
如果有人在使用 shm_get_var() 時遇到問題:

這段程式碼可以正確處理布林值^
<?php
if(shm_has_var ($this->shm_mem_id, $_varno)){
$_value = shm_get_var($this->shm_mem_id, $_varno);
}else{
$_value = NULL;
}
?>
To Top