php Memcache class can not be found
all I got a problem when try to use memcache with php I hava successfully install php and memcached extension. I write a memtest.php to test it and it success. Here is the test.
connect('xxxxxx', 11211) or die ("Could not connect"); $memcache->set('key', 'test'); $get_value = $memcache->get('key'); echo $get_value; ?>
It echo test. But when I used it in my php web server, I failed. Here is my code: I wrote a memcacheTool.php
memcache = new Memcache(); > $res = $this -> memcache -> connect($cacheServer, $cachePort); if( false == $res) < $this ->memcache = null; > > public static function getInstance() < if (is_null( self::$_instance)) < self::$_instance = new self(); >return self::$_instance; > /** * set operation * * @param string $key * @param string $value */ static function set($key, $value) < //error_log("memcache: set ".$key"value ".$value); if ( !trim($key) ) < return false; >$memcache -> add($key,$value,MEMCACHE_COMPRESSED,0); > /** * get operation * * @param string $key */ static function get($key) < if ( !trim($key) ) < return null; >if (is_null($memcache -> get($key))) < return null; >$memValue = $memcache -> get($key); return $memValue; > >
function __autoload($object) < require("". $object . ".php"); if ( !class_exists($object) ) < error_log("Class loader failed to load class " . $object); include_once("app/http.php"); include_once("app/poster.php"); include_once("app/handle.php"); include_once("app/loader.php"); include_once("app/casserver.php"); return; > >
function __autoload($object) < if ($object != "Memcache") < require("". $object . ".php"); >if ( !class_exists($object) ) < error_log("Class loader failed to load class " . $object); include_once("app/http.php"); include_once("app/poster.php"); include_once("app/handle.php"); include_once("app/loader.php"); include_once("app/casserver.php"); return; > >
abviously,The Memcache class can not be found,but why the test php is success? I wish someone could help me, cause I am really a newer with php.
PHP memcached Fatal error: Class ‘Memcache’ not found
So php seems to have loaded memcached as a module, and php info says that it is loaded and is using v .37. I have not tried yet via apache, I’ve only been using this through cli right now. Any thoughts?
I just took a look at that and I already have the extension listed in my php.ini and I’ve already ensured my extension_dir is pointed to the correct place.
sudo apt-get install php-memcache is what worked for me. packages.ubuntu.com/… shows available packages.
I hve the same problem with php 8.1 and php8.2 right now. It seems like the object-cache.php file is not compatible any more?
7 Answers 7
There are two extensions for memcached in PHP, «memcache» and «memcached».
It looks like you’re trying to use one («memcache»), but the other is installed («memcached»).
In the ~13 years since this answer was written, much has changed in the PHP community. The old memcache extension seems to be unmaintained, so please use the memcached extension instead if you can. It’s a better option regardless! See this other answer below for up to date installation and build links.
It is kind of silly, but it’s an unfortunately frequent pattern in PHP. Rather than updating the older extension to work with newer things in better ways, but breaking backwards compt, they simply created a new extension. See also mysql and mysqli.
Ok, I found a simple fix (since everything else seemed to not work). Just run this command: sudo aptitude install memcached php5-memcached
@rubo77 — It does, according to the PECL site for memcache. See also the post below: stackoverflow.com/a/76093380/19980636
@Krokomot It’s been thirteen years and eight days since I wrote this answer, and back then memcache and memcached were both active and maintained. I’ve added a note about memcache being unmaintained and linked to your post. Thanks for diving in!
Dispite what the accepted answer says in the comments, the correct way to install ‘Memcache’ is:
sudo apt-get install php5-memcache
NOTE Memcache & Memcached are two distinct although related pieces of software, that are often confused.
EDIT As this is now an old post I thought it worth mentioning that you should replace php5 with your php version number.
A couple years later, I think this answer is outdated. sudo apt-get install php-memcache is what worked for me. packages.ubuntu.com/… shows available packages.
@Ryan Surely, that only applies if not using PHP5. Plenty of servers still use PHP5 so it’s just as valid as it was at the time. Additionally the question is tagged ubuntu-9.10 so this further validates my answer since it relates directly to that version.
I found the working dll files for PHP 5.4.4
I don’t knowhow stable they are but they work for sure. Credits goes to this link.
It is the 2.2.5.0 version, I noticed after compiling it (for PHP 5.4.4).
Please note that it is not 2.2.6 but works. I also mirrored them in my own FTP. Mirror links:
Run the following command to install Memcached:
To avoid confusion as this is an older question, a small prologue:
rubo77 refreshed this question by placing a bounty and asking for a canonical answer. Reading the little bit old but nevertheless still valid answers — see esp. the accepted one — , I realized that indeed some basic but important, so to say canonical, links miss.
So here shall be provided not a canonical answer as such — which could change in details over the years — but canonical links which will provide the answer.
The Missing Link
Sorry for the fossil joke ^^. Ok, back to serious. The main canonical source to answer the question is of course php.net, and more precisely:
- Memcache Installation Guide, last updated 2022 by an accepted user contribution considering PHP 7.4
- Memcached Installation Guide, last updated 2020 by an accepted user contribution considering PHP 7.2.19
The extensive and up-to-date ServerPilot installation guide for Memcached as well as Memcache is also pretty useful.
Naturally, the question comes up — What about PHP8? Lets take a look into the respective repository, the PHP Extension Community Library (PECL), and we find:
- The last Memcache package stable release 8.0 from 2020-12-06 depends on PHP>=8.0.0. For PHP>=7.0 take release 4.0.5.2 from 2019-12-20.
- The last Memcached package stable release 3.2.0 from 2022-03-24 depends on PHP>=7.0.0, so can also be used with PHP8.
Note, that Memcache (without ‘d’) is no longer maintained.
So these sources should be the very first places to look for (after Stackoverflow, of course), before starting a deeper web search campaign (which won’t yield that much more, tbh).
Memcached not working in web browser,but use php command working
I run testMemcached.php in the web browser. I get bool(false) Value: . I run php -f testMemcached.php command then get the output below.
array(1) < ["localhost:11211"]=>array(24) < ["pid"]=>int(10218) . (skip) ["version"]=> string(6) "1.4.15" > > Value:test
I don’t know what the difference is and how to fix memcached not working in the web browser. My environment:CentOS 7. LNMP. 2018/05/23 Update : I use telnet 127.0.0.1 11211 to test memcached function I found the add and set is not working.
Trying 127.0.0.1. Connected to 127.0.0.1. Escape character is '^]'. set test testValue ERROR add test testValue ERROR get test END
This is my memcached setup from phpinfo below. I use getResultCode() code below to find some error This is my test result output.
MemcachedFunction ResultCode ErrorDescription stats 3 MEMCACHED_CONNECTION_FAILURE set 3 MEMCACHED_CONNECTION_FAILURE add 47 MEMCACHED_SERVER_TEMPORARILY_DISABLED get 47 MEMCACHED_SERVER_TEMPORARILY_DISABLED fetchAll 16 MEMCACHED_NOTFOUND
setOption(Memcached::OPT_BINARY_PROTOCOL, true); $m->addServer("localhost","11211"); $stats = $m->getStats(); echo "stats ".$m->getResultCode()."
"; // stats 3 var_dump($stats); // bool(false) echo "
"; $m->set("Key","test"); echo "set ".$m->getResultCode()."
"; // set 3 $m->add("Key","test"); echo "add ".$m->getResultCode()."
"; // add 47 echo "Value:".$m->get("Key")."
"; // Value: echo "get ".$m->getResultCode()."
"; // get 47 var_dump($m->fetchAll()); // bool(false) echo "
"; echo "fetchAll ".$m->getResultCode()."
"; // fetchAll 16 var_dump($m->getAllKeys()); // bool(false)
Memcached not showing up in phpinfo()
I’ve installed libmemcached and memcached pecl extension for php and for some reason it’s not installing correctly? i’ve got memcached.so in /usr/lib64/php/ with the right permissions and libmemcache.so in /usr/local/lib/ Everything seemed to build correctly without error, and I restarted apache? i also have the daemon installed. I somehow easily got the Memcache class easily installed for php before, but I realized what i wanted was the Memcached (note the d) class. let me know if more info is needed! EDIT: I previously had memcache (without the d) working in php so I know i was manipulating the correct php.ini! EDIT 2: there WAS indeed an apache error!
Unable to load dynamic library '/usr/lib64/php/modules/memcached.so' - /usr/lib64/php/modules/memcached.so: undefined symbol: php_json_encode in Unknown on line 0
Simply fix the extension load order, json before memcached. (Using distribution packages, each extension have its configuration file starting with a number ensuring proper load order)
5 Answers 5
I’ve solved it in centos5 using the yum installer instead of the pecl install memcached
THIS way —> yum install php-pecl-memcached
then, service httpd restart and it shows up in phpinfo()
I have the similar error few days ago. Instaling of new version(2.0) of memcached helped me.
yum -y install gcc-c++ wget http://launchpad.net/libmemcached/1.0/0.50/+download/libmemcached-0.50.tar.gz tar xzf libmemcached-0.50.tar.gz cd libmemcached-0.50 ./configure make make install wget http://pecl.php.net/get/memcached-2.0.0b2.tgz pecl install memcached-2.0.0b2.tgz echo 'extension=memcached.so' > /etc/php.d/memcached.ini service php-fpm restart cd .. rm -r libmemcached-0.50* rm -r memcached-
I don’t know if it’s the same problem but after struggling with this for hours I manage to have PHP properly installed with Memcached on CentOS using the command bellow:
yum --enablerepo=remi-php73 install php-memcached
Should show something like:
/etc/php.d/50-memcached.ini memcached memcached support => enabled libmemcached version => 1.0.18 memcached.compression_factor => 1.3 => 1.3 memcached.compression_threshold => 2000 => 2000 memcached.compression_type => fastlz => fastlz memcached.default_binary_protocol => Off => Off memcached.default_connect_timeout => 0 => 0 memcached.default_consistent_hash => Off => Off memcached.serializer => igbinary => igbinary memcached.sess_binary_protocol => On => On memcached.sess_connect_timeout => 0 => 0 memcached.sess_consistent_hash => On => On memcached.sess_consistent_hash_type => ketama => ketama memcached.sess_lock_expire => 0 => 0 memcached.sess_lock_max_wait => not set => not set memcached.sess_lock_retries => 5 => 5 memcached.sess_lock_wait => not set => not set memcached.sess_lock_wait_max => 150 => 150 memcached.sess_lock_wait_min => 150 => 150 memcached.sess_locking => On => On memcached.sess_number_of_replicas => 0 => 0 memcached.sess_persistent => Off => Off memcached.sess_prefix => memc.sess.key. => memc.sess.key. memcached.sess_randomize_replica_read => Off => Off memcached.sess_remove_failed_servers => Off => Off memcached.sess_sasl_password => no value => no value memcached.sess_sasl_username => no value => no value memcached.sess_server_failure_limit => 0 => 0 memcached.store_retry_count => 2 => 2 Registered save handlers => files user memcached