Skip to content

Instantly share code, notes, and snippets.

@mcpar-land
Last active January 27, 2025 19:43
Show Gist options
  • Select an option

  • Save mcpar-land/5ecf6e0f4279a8b508f0f1a4bb491f44 to your computer and use it in GitHub Desktop.

Select an option

Save mcpar-land/5ecf6e0f4279a8b508f0f1a4bb491f44 to your computer and use it in GitHub Desktop.
initialize static PHP variable
<?php
class STATIC_CLASS_TEST {
static $foo;
static function init() {
self::$foo = ['foo', 'bar', 'baz', $_ENV['MY_ENV_VARIABLE']];
}
}
STATIC_CLASS_TEST::init();
echo "the value of foo is: ";
echo json_encode(STATIC_CLASS_TEST::$foo);
echo "\n";
php test.php
MY_ENV_VARIABLE=woomy php test.php
nix-shell -p php --run ./test.sh
Warning: Undefined array key "MY_ENV_VARIABLE" in **/test.php on line 7
the value of foo is: ["foo","bar","baz",null]
the value of foo is: ["foo","bar","baz","woomy"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment