Content · Programming · PHP

PHP AMQPConnection::disconnect() Method: Usage and Examples

Closes the active AMQP connection after its operations have finished. Disconnecting before a publish is confirmed risks losing messages; close channels cleanly.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Closes the active AMQP connection after its operations have finished.

The examples focus on the operation itself, including the important distinction: connect opens the broker connection.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Syntax and Return Value


AMQPConnection::disconnect(): void

The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.

Example 1: Basic Use


Requires the PECL amqp extension and a reachable configured broker. Do not assume the broker exists on a plain PHP installation; transactions apply only to the AMQP channel.

<?php
$connection = new AMQPConnection([
    "host" => getenv("AMQP_HOST") ?: "127.0.0.1",
    "login" => getenv("AMQP_USER") ?: throw new RuntimeException("AMQP_USER is required"),
    "password" => getenv("AMQP_PASSWORD") ?: throw new RuntimeException("AMQP_PASSWORD is required"),
]);
$connection->connect();
$connection->disconnect();
echo $connection->isConnected() ? "open" : "closed";

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 2: Checking Behavior


Requires the PECL amqp extension and a reachable configured broker. Do not assume the broker exists on a plain PHP installation; transactions apply only to the AMQP channel. The AMQPConnection object reports local connection state, not guaranteed remote broker health.

<?php
$connection = new AMQPConnection([
    "host" => getenv("AMQP_HOST") ?: "127.0.0.1",
    "login" => getenv("AMQP_USER") ?: throw new RuntimeException("AMQP_USER is required"),
    "password" => getenv("AMQP_PASSWORD") ?: throw new RuntimeException("AMQP_PASSWORD is required"),
]);
$connection->connect();
$connection->disconnect();
echo "connection closed=" . ($connection->isConnected() ? "no" : "yes");

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 3: Practical Example


Requires the PECL amqp extension and a reachable configured broker. Do not assume the broker exists on a plain PHP installation; transactions apply only to the AMQP channel. The AMQPConnection object reports local connection state, not guaranteed remote broker health.

<?php
$connection = new AMQPConnection([
    "host" => getenv("AMQP_HOST") ?: "localhost",
    "login" => getenv("AMQP_USER") ?: throw new RuntimeException("AMQP_USER is required"),
    "password" => getenv("AMQP_PASSWORD") ?: throw new RuntimeException("AMQP_PASSWORD is required"),
]);
$connection->connect();
$connection->disconnect();
echo $connection->isConnected() ? "open" : "closed";
echo "amqp lifecycle checked";

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Important Behavior and Edge Cases


Disconnecting before a publish is confirmed risks losing messages; close channels cleanly.

This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.

Comparison with Related Approaches


ApproachDifference
AMQPConnection::disconnectCloses the active AMQP connection after its operations have finished.
Related API or alternativeconnect opens the broker connection.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsDisconnecting before a publish is confirmed risks losing messages; close channels cleanly.
Confusing it with a related APIconnect opens the broker connection.

Key Takeaways


  • Closes the active AMQP connection after its operations have finished.
  • Disconnecting before a publish is confirmed risks losing messages; close channels cleanly.
  • connect opens the broker connection.

Use AMQPConnection::disconnect when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.