mirror of
https://codeberg.org/vlw/php-globalsnapshot.git
synced 2025-09-13 20:03:41 +02:00
Change namespace from `victorwesterlund/globalsnapshot` to `vlw/globalsnapshot`. I will also deprecate the [existing Packagist listing](https://packagist.org/packages/victorwesterlund/globalsnapshot) in favor of a new one using the new namespace. Reviewed-on: https://codeberg.org/vlw/php-globalsnapshot/pulls/3 Co-authored-by: vlw <victor@vlw.se> Co-committed-by: vlw <victor@vlw.se>
1.1 KiB
1.1 KiB
php-globalsnapshot
Capture the current state of all PHP superglobal variables; which can then be restored to that state at a later time.
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
- Install with composer
composer install vlw/globalsnapshot
use
in your project
use vlw\GlobalSnapshot;
- 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();
- Restore superglobals with
restore()
// ... some other code
// Restore superglobals to state of `capture()`
$snapshot->restore();