Can anyone explain how this works:
${(int)!${0}=$variable->other['class']}::my_static_method();
in the answer of this stackoverflow question:
It does the same as:
${0}=$variable->other['class'];//value here lets say is 'myClass'
${0}::my_static_method();//here now is myClass::my_static_method(); called
Why is (int)! used?
Because he just tricks around.
Result of print (int)!${0}=$variable->other['class']; is 0.
Because if anything is set here ${0}=$variable->other['class'],
print (bool)${0}=$variable->other['class']; will be true.
Then it will be negated via !, so it becomes false and with (int) finally 0.
And thats the name of the variable that was set before: ${0}.
But it is very bad practise to give an global variable just an number.
Normaly all variables MUST start with _ or A-Za-z.
Dont know why its droped as example, maybe to make fun of the people.
Dont use this at all!
Just to show how its not done ;) (working code)
error_reporting(0);
define(0x7F,' ..the crazy Girl');
define('§','at? ');
define('_',' Wh');
${'$#?!'} = ' Arrr!';
$♂ = Tom;
$♀ = Tina;
$c = get_defined_constants();
print _ . § . $♂ .' '. Just . ' ' . Want . ' ' . To ." ". Lov€ ." ". $♀ . $c[127] . ${'$#?!'};
//result: What? Tom Just Want To Lov€ Tina ..the crazy Girl Arrr!
Feel free to get crazy. :-)
Its a variable variable statement, creating a global variable name using the boolean statement result in the first ${ }
${
(int)
!
${0}
$0=
$0 variable.$variable->other['class']
}
::
So in Layman's terms, it could create the following statement:
$1::my_static_method();
Why a digit? Because the variable name is created by a boolean statement, that is either true or false, it is inverted and cast to an integer.
And in PHP true and false equals 1 and 0.
So $1 on failure and $0 on success.
Overall, keep in mind that you are bypassing the PHP interpreter for syntax errors. While the feature of variable variables is intended to create dynamic variable names I do not believe they meant it to be abused like this as a result it could mean your code could break with newer versions of PHP. An example would be the answer of JustOnUnderMillions where the result in PHP 7 is What? Tom Just Want To Lov€ Tina Arrr!
Just saying, using variable variable statements is bad programming practice anyways.