Corveda/PHPSandbox

named argument not working

Opened this issue · 7 comments

Fatal error: Cannot use positional argument after named argument in /data/data/com.termux/files/home/php/bota/vendor/corveda/php-sandbox/src/PHPSandbox.php(6990) : eval()'d code on line 28

Which PHP version you use?

Which PHP code snippets you execute via the PHPSandbox?

  1. Php8.2
  2. $myClass->method(name: "John", age: 50);

I think the fatal error is happened because it will execute the eval('$myClass->method(name: "John", age: 50);') code via the PHPSandbox.

I need to setup the PHPSandbox and verify above explanation.

Ok I will be waiting thanks

I think the fatal error is happened because it will execute the eval('$myClass->method(name: "John", age: 50);') code via the PHPSandbox.

I need to setup the PHPSandbox and verify above explanation.

The explanation is confirmed. And I use the following code snippets to get the same fatal error:

PHP Fatal error: Cannot use positional argument after named argument in /home/peterli/vendor/corveda/php-sandbox/src/PHPSandbox.php(6990) : eval()'d code on line 5

<?php

require_once __DIR__ . '/vendor/autoload.php';


class MyClass
{
    public function method($name, $age) {
        return [$name, $age];
    }
}
$class = new MyClass;

$codeSnippets = '<?php var_dump($class->method(name: \'Peter\', age: 50));';


$sandbox = new PHPSandbox\PHPSandbox;
$sandbox->defineVars(['class' => $class]);
$sandbox->whitelistClass(MyClass::class);
$sandbox->whitelistFunc('var_dump');
$sandbox->execute($codeSnippets);

To avoid getting the above fatal error you mention, I think the temporary way is to use the positional argument to call the method:

<?php

require_once __DIR__ . '/vendor/autoload.php';


class MyClass
{
    public function method($name, $age) {
        return [$name, $age];
    }
}
$class = new MyClass;

$codeSnippets = '<?php var_dump($class->method(\'Peter\', 50));';


$sandbox = new PHPSandbox\PHPSandbox;
$sandbox->defineVars(['class' => $class]);
$sandbox->whitelistClass(MyClass::class);
$sandbox->whitelistFunc('var_dump');
$sandbox->execute($codeSnippets);

Ok thank you for your time, will you fix the issue in future?

I think it's not easy to fix the issue because it has the compatibility issue.

It needs to use the named argument approach when calling the eval function. And the feature is only available for the PHP 8.0 version at least.