我需要什么
代码段
class Cache_model extends CI_Model {
public function __construct()
{
parent::__construct ();
$this->load->model('memcache_model');
$m = new Memcached();
$m->addServer('localhost', 11211);
$this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'file'));
}
// function that works
public function getCatLevelInfo($cat_id, $cat_index)
{
$Data = array();
$m = new Memcached();
$m->addServer('localhost', 11211);
$cat_info = $m->get($cat_index.$cat_id);
}
}
// function that don"t works
public function getCatLevelInfo($cat_id, $cat_index)
{
$Data = array();
$cat_info = $this->get($cat_index.$cat_id);
}
答案 0 :(得分:2)
您需要将$m
声明为全局变种
<?php
/**
* Created by PhpStorm.
* User: Ilan Hasanov
* Date: 1/7/2016
* Time: 1:02 PM
*/
class Cache_model extends CI_Model {
private $m = null;
public function __construct()
{
parent::__construct();
$this->load->model('memcache_model');
$this->m = new Memcached();
$this->m->addServer('localhost', 11211);
$this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'file'));
}
// function that works
public function getCatLevelInfo($cat_id, $cat_index)
{
$cat_info = $this->m->get($cat_index.$cat_id);
var_dump($cat_info);
}
}