Skip to main content
added 52 characters in body
Source Link
MSpreij
  • 1.2k
  • 13
  • 20

You can't call functions inside {} directly like this, but you can use variables-holding-the-function-name and call those! (*and* you can use variable variables on it, too)

You can't call functions inside {} directly like this, but you can use variables-holding-the-function-name and call those!

You can't call functions inside {} directly like this, but you can use variables-holding-the-function-name and call those! (*and* you can use variable variables on it, too)

another addition
Source Link
MSpreij
  • 1.2k
  • 13
  • 20

Found this little gem in Reddit comments:

$foo = 'abcde';
$strlen = 'strlen';
echo "$foo is {$strlen($foo)} characters long."; // "abcde is 5 characters long."

You can't call functions inside {} directly like this, but you can use variables-holding-the-function-name and call those!

Found this little gem in Reddit comments:

$foo = 'abcde';
$strlen = 'strlen';
echo "$foo is {$strlen($foo)} characters long."; // "abcde is 5 characters long."

You can't call functions inside {} directly like this, but you can use variables-holding-the-function-name and call those!

added 416 characters in body
Source Link
MSpreij
  • 1.2k
  • 13
  • 20

So, just discovered you can pass any string as a method name IF you enclose it with curly brackets. You can't define any string as a method alas, but you can catch them with __call(), and process them further as needed. Hmmm....

class foo {
  function __call($func, $args) {
    eval ($func);
  }
}

$x = new foo;
$x->{'foreach(range(1, 10) as $i) {echo $i."\n";}'}();

So, just discovered you can pass any string as a method name IF you enclose it with curly brackets. You can't define any string as a method alas, but you can catch them with __call(), and process them further as needed. Hmmm....

class foo {
  function __call($func, $args) {
    eval ($func);
  }
}

$x = new foo;
$x->{'foreach(range(1, 10) as $i) {echo $i."\n";}'}();
added 177 characters in body
Source Link
MSpreij
  • 1.2k
  • 13
  • 20
Loading
removed escape slashes
Source Link
MSpreij
  • 1.2k
  • 13
  • 20
Loading
code formatting
Source Link
Kyle Cronin
  • 79.4k
  • 45
  • 152
  • 167
Loading
Post Made Community Wiki
Source Link
MSpreij
  • 1.2k
  • 13
  • 20
Loading