note 91728 added to function.shm-get-var

From: Date: Wed, 24 Jun 2009 09:19:21 +0000
Subject: note 91728 added to function.shm-get-var
Groups: php.notes 
Request: Send a blank email to php-notes+get-157334@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 69.147.83.197 Probable Submitter: 217.7.242.172 ---- Manual Page -- http://www.php.net/manual/en/function.shm-get-var.php Edit -- https://master.php.net/note/edit/91728 Del: integrated -- https://master.php.net/note/delete/91728/integrated Del: useless -- https://master.php.net/note/delete/91728/useless Del: bad code -- https://master.php.net/note/delete/91728/bad+code Del: spam -- https://master.php.net/note/delete/91728/spam Del: non-english -- https://master.php.net/note/delete/91728/non-english Del: in docs -- https://master.php.net/note/delete/91728/in+docs Del: other reasons-- https://master.php.net/note/delete/91728 Reject -- https://master.php.net/note/reject/91728 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157334) next »