Skip to content

Instantly share code, notes, and snippets.

@aheadley
Last active August 29, 2015 14:12
Show Gist options
  • Save aheadley/9bb10bd0a0cbd60981d4 to your computer and use it in GitHub Desktop.
Save aheadley/9bb10bd0a0cbd60981d4 to your computer and use it in GitHub Desktop.

Most of this is pretty simple, but we'll walk through the whole thing for the sake of completeness. Starting with the inner bit

return ${ ! ${''} = (new ReflectionClass( get_called_class() ) ) }->newInstanceArgs( func_get_args() );

return ${ ! ${''} = (new ReflectionClass( 'A' ) ) }->newInstanceArgs( func_get_args() );

return ${ ! ${''} = ReflectionClass('A') }->newInstanceArgs( func_get_args() );

The next bit is the magic, ${<identifier>} lets you access a variable with the name <identifier>, so with ${''} a variable with a name of '' is being accessed. Or in this case, the Reflection object is being assigned to the empty string variable. It is essentially equivilent to $GLOBALS[''] = ... .

return $GLOBALS[ ! $GLOBALS[''] = (ReflectionClass('A')) ]->newInstanceArgs( func_get_args() );

The empty string var is then negated with !, and since non-empty objects are truthy in PHP, it evaluates as false. Then the outer variable syntax converts the false value to a string to get a variable name, and false converts to an empty string, so the variable just created is accessed.

class A {
public static function f()
{
return ${!${''}=(new ReflectionClass(get_called_class()))}->newInstanceArgs(func_get_args());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment