PHP memcached Fatal error: Class 'Memcache' not found
When you encounter a Fatal error: Class 'Memcache' not found in your PHP application, it is almost never because the underlying C extension is missing from your installation. In fact, as seen in your diagnostic output, the memcached module is explicitly listed in both php.ini and the result of php -m. The core libraryâlibmemcached 0.37âis loaded and functioning at the system level.
The issue stems from a naming convention mismatch between the underlying C extension and the PHP wrapper class you are trying to instantiate. The C extension provided by the PECL package is named memcached, but the legacy PHP API exposed by that extension was historically named Memcache. Modern PHP versions deprecated this legacy API in favor of the new Memcached class, which offers a more robust interface and better support for distributed hashing strategies.
If your code attempts to instantiate new Memcache(), PHP will look for the old, deprecated wrapper. While this used to work transparently when the extension was named memcache.so, the separation between the extension name (memcached) and the class name (Memcache vs. Memcached) can lead to confusion. More critically, if you are running a version of PHP where the legacy Memcache class has been fully removed or if your extension is strictly enforcing the new naming scheme, the class simply does not exist in the global namespace.
To resolve this, you must update your instantiation logic to use the modern Memcached class. This change provides immediate benefits beyond just fixing the fatal error. The Memcached class supports multiple servers natively and handles failover more gracefully than its predecessor. Consider updating your connection code as follows:
$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
$memcache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
By switching to the Memcached class, you gain access to features like consistent hashing (memcache.hash_strategy = "consistent" in your config) without needing workarounds. The configuration you have set upâspecifically memcache.allow_failover and memcache.max_failover_attemptsâis still valid for the new class, though the property names and some option constants have shifted slightly to match the new API standards.
It is also worth noting that this discrepancy often arises when mixing documentation from older PHP resources with current server environments. The example from php.net you referenced likely used the legacy Memcache class, which implies compatibility with older versions of the PECL extension or specific PHP releases that still bridged the gap between the two names. However, relying on that legacy bridge is risky. If your production environment updates to a newer PHP version where the legacy API is stripped out entirely, your application will break immediately.
Furthermore, ensure that you are not inadvertently loading an older version of the extension via pecl install memcache instead of pecl install memcached. While they serve similar purposes, the modern Memcached extension provides a class with the same name as the extension, reducing ambiguity. If you are using a framework like Laravel, this issue might also manifest if your service provider is attempting to register an old client. Always verify that your dependency manager (composer) requires a version of the memcached package compatible with your PHP version.
Finally, remember that php -i confirms the extension is loaded, but it does not guarantee that the specific class interface you expect is available. The presence of the module in the output confirms the C library is active, but the fatal error indicates that the PHP wrapper exposing the legacy API has not been initialized or has been replaced. Updating your code to use Memcached aligns your application with current best practices and ensures stability across different PHP versions and server configurations.
