Same name and namespace in other branches
  1. 7.x includes/bootstrap.inc \drupal_register_shutdown_function()
  2. 8.9.x core/includes/bootstrap.inc \drupal_register_shutdown_function()
  3. 9 core/includes/bootstrap.inc \drupal_register_shutdown_function()

Registers a function for execution on shutdown.

Wrapper for register_shutdown_function() that catches thrown exceptions to avoid "Exception thrown without a stack frame in Unknown".

Parameters

callable $callback: The shutdown function to register.

...: Additional arguments to pass to the shutdown function.

Return value

array Array of shutdown functions to be executed.

See also

register_shutdown_function()

Related topics

10 calls to drupal_register_shutdown_function()
Database::commitAllOnShutdown in core/lib/Drupal/Core/Database/Database.php
Calls commitAll() on all the open connections.
DatabaseLockBackend::__construct in core/lib/Drupal/Core/Lock/DatabaseLockBackend.php
Constructs a new DatabaseLockBackend.
KernelTestBaseShutdownTest::shutdownFunction in core/tests/Drupal/KernelTests/KernelTestBaseShutdownTest.php
Registers that this shutdown function has been called.
KernelTestBaseShutdownTest::testShutdownFunction in core/tests/Drupal/KernelTests/KernelTestBaseShutdownTest.php
@covers ::assertPostConditions
SessionTestController::triggerWriteException in core/modules/system/tests/modules/session_test/src/Controller/SessionTestController.php
Trigger an exception when the session is written.

... See full list

1 string reference to 'drupal_register_shutdown_function'
Database::commitAllOnShutdown in core/lib/Drupal/Core/Database/Database.php
Calls commitAll() on all the open connections.

File

core/includes/bootstrap.inc, line 513
Functions that need to be loaded on every Drupal request.

Code

function &drupal_register_shutdown_function($callback = NULL) {

  // We cannot use drupal_static() here because the static cache is reset during
  // batch processing, which breaks batch handling.
  static $callbacks = [];
  if (isset($callback)) {

    // Only register the internal shutdown function once.
    if (empty($callbacks)) {
      register_shutdown_function('_drupal_shutdown_function');
    }
    $args = func_get_args();

    // Remove $callback from the arguments.
    unset($args[0]);

    // Save callback and arguments
    $callbacks[] = [
      'callback' => $callback,
      'arguments' => $args,
    ];
  }
  return $callbacks;
}