Home  >  Article  >  Backend Development  >  服务器一变量多个用户可以同时修改,怎么实现

服务器一变量多个用户可以同时修改,怎么实现

WBOY
WBOYOriginal
2016-06-13 13:13:49817browse

服务器一变量多个用户可以同时修改,如何实现?
例如:我设置变量$a,
多个会员,调用$a时,都为同一对象,而不是单独的。但是不能用数据库实现。
$a=100;

当张三,访问$a,把$a改为5;
这时,李四访问$a,看到$a为5,李四把$a改为50,

多个用户可以修改$a,多可以看到。类似共享的变量。

如何实现?

------解决方案--------------------
基于 memcache 的公共变量类

PHP code
class TShare {
  private static $_Instance;
  private $memcache;
  private function __construct() {
    $this->memcache = new Memcache;
    $this->memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
  }
  public static function getInstance() {
    if(empty(self::$_Instance)) self::$_Instance = new self();
    return self::$_Instance;
  }
  function __set($name, $val) {
    $this->memcache->set($name, $val, false, 1000);
  }
  function __get($name) {
    return $this->memcache->get($name);
  }
} <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn