Capture and restore the current state of all PHP superglobals.
Find a file
vlw 4bb9e4a08c chore: add PHP 8.1 readonly properties (#4)
Reviewed-on: https://codeberg.org/vlw/php-globalsnapshot/pulls/4
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2024-11-23 09:23:42 +00:00
src chore: add PHP 8.1 readonly properties (#4) 2024-11-23 09:23:42 +00:00
.gitignore Initial code commit 2024-04-18 13:23:30 +02:00
composer.json chore: change project namespace (#3) 2024-11-23 09:23:17 +00:00
LICENSE Initial commit 2024-04-18 11:06:34 +00:00
README.md chore: add PHP 8.1 readonly properties (#4) 2024-11-23 09:23:42 +00:00

php-globalsnapshot

Capture the current state of all PHP superglobal variables - which can then be restored to the state of capture at a later time.

Supports PHP 8.1+

Example use:

// Initial state
$_ENV["hello"] = "world"; // echo: "world"

// Capture initial state
$snapshot = (new GlobalSnapshot())->capture();

// Manipulate superglobals
$_ENV["hello"] .= " and mom!"; // echo: "world and mom"

// Restore initial state
$snapshot->restore();

// Initial state restored
echo $_ENV["hello"]; // echo: "world"

Quickstart

  1. Install with composer
composer install vlw/globalsnapshot
  1. use in your project
use vlw\GlobalSnapshot;
  1. Capture current superglobals with capture()
// Initialize a new GlobalSnapshot instance to store current values (one snapshot per instance)
$snapshot = new GlobalSnapshot();

// Capture current superglobal state
$snapshot->capture();
  1. Restore superglobals with restore()
// ... some other code

// Restore superglobals to state of `capture()`
$snapshot->restore();