Created
February 4, 2011 17:11
-
-
Save Zegnat/811387 to your computer and use it in GitHub Desktop.
CASSIS: Scooping
This file contains 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
// Javascript needs you to declare variables inside functions or they will be created in the global scoop. | |
$var1 = 'global'; | |
function change() { | |
if (js()) eval('var $var1;'); | |
$var1 = 'scooped'; | |
} | |
change(); | |
// PHP needs you to declare variables inside functions if you want them to be in the gobal scoop. | |
$var2 = 'global'; | |
function change2() { | |
if (!js()) eval('global $var2;'); | |
$var2 = 'scooped'; | |
} | |
change2(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To think about when writing CASSIS code:
Keep within a function.
Without line 4
$var1
will equalglobal
in PHP andscooped
in Javascript. When line 4 is included$var1
equalsglobal
in both cases. The value from inside the function no longer leaks into the global scoop.Accessing global scoop.
Without line 12
$var2
will equalglobal
in PHP andscooped
in Javascript. When line 12 is included$var2
equalsscooped
in both cases. The value from inside the function correctly overwrites the global scoop.