0

Can anyone explain how this works:

${(int)!${0}=$variable->other['class']}::my_static_method();

in the answer of this stackoverflow question:

error when using variable class name and static method

2 Answers 2

3

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. :-)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. It dawned on me as I read through this how it was working.
"Dont use this at all!" +1 just for that
So this will also work in the same way: ${'a' . substr($a = $variable->other['class'], 0, 0)}::my_static_method();
@Capsland Yes. You substring nothing so you become a again. Read this also: wiki.php.net/rfc/uniform_variable_syntax
@CapIsland Mark that as answer if it was THE answer you need, thnx
2

Its a variable variable statement, creating a global variable name using the boolean statement result in the first ${ }

  • ${
    • Create a variable variable.
  • (int)
    • Cast result of following statement to integer
  • !
    • Not statement, invert boolean result.
  • ${0}
    • create a variable $0
    • this would normally cause a parse error where a T_VARIABLE is expected instead of a T_LNUMBER
  • =
    • set $0 variable.
  • $variable->other['class']
    • this is an object variable, defined somewhere else, what it does or what it holds I do not know.
  • }
    • End of the first variable variable name.
  • ::
    • Call a static method from the class name created by the boolean statement

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.

1 Comment

Thanks. Nice explanation also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.