问题
I installed memcached in a CentOS following this steps
This is the PHP error:
Fatal error: Call to a member function get() on a non-object in /home/piscolab/public_html/keepyourlinks.com/includes/funciones.php on line 22
Code:
/* MEMCACHE */
$memcache = new Memcache();
$memcache->pconnect('localhost',11211);
/* checks in memcache, if not: query and save to memcache */
function cache_query($sql,$nombre,$tiempo = -1){
if($tiempo == -1) $tiempo = time() + 24*60*60*365;
$consulta = $memcache->get($nombre); /* THIS is the line */
if ( $consulta === false) {
$consulta = mysql_query($sql);
$memcache->set($nombre,$consulta,0,$tiempo);
$_SESSION['tQ']++;
}else $_SESSION['tQC']++;
return $consulta;
}
I call the function so:
$result = cache_query('select * from users','all_users');
What am I missing?
回答1:
The $memcache object is out of scope here.
Add the following line to the top of your function:
global $memcache;
Or pass it as a parameter:
function cache_query($sql, $nombre, $memcache, $tiempo = -1) {
...
}
$result = cache_query('select * from users','all_users', $memcache);
回答2:
You need to do:
global $memcache;
In your function.
See this for info on variable scope:
http://php.net/manual/en/language.variables.scope.php
来源:https://stackoverflow.com/questions/10776545/memcache-call-to-a-member-function-get-on-a-non-object