php8.5
Home/ Manual/ memcached / memcached/ Memcached::__construct

Memcached::__construct

PHP function Edit on GitHub ✎

(PECL memcached >= 0.1.0)

Create a Memcached instance

Description

Memcached::__construct(string|null $persistent_id = null, callable|null $callback = null, string|null $connection_str = null)

Creates a Memcached instance representing the connection to the memcache servers.

Func

Parameters

persistent_id

By default the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.

callback
callback(Memcached $memcached, string|null $persistent_id): void

The callback parameter is called when the connection is established. It should be a valid PHP callable that receive the Memcached object as its first parameter, and persistent_id as its second.

connection_str

This parameter is used to pass additional connection options to the memcache servers, such as a server weight in a cluster.

Examples

Creating a Memcached object

php
<?php
/* Create a regular instance */
$m = new Memcached();
echo get_class($m);

/* Create a persistent instance */
$m2 = new Memcached('story_pool');
$m3 = new Memcached('story_pool');

/* now $m2 and $m3 share the same connection */
?>

Source: reference/memcached/memcached/construct.xml · from the official PHP manual (php/doc-en)