Last active
January 27, 2025 19:43
-
-
Save mcpar-land/5ecf6e0f4279a8b508f0f1a4bb491f44 to your computer and use it in GitHub Desktop.
initialize static PHP variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| php test.php | |
| MY_ENV_VARIABLE=woomy php test.php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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