我正在尝试理解如何在Process类中覆盖__construct。默认情况下,构造只接受数组作为第一个参数:
public function __construct(array $command, string $cwd = null, array $env = null, mixed $input = null, ?float $timeout = 60)但我也需要它来接受字符串,意思是这样的
public function __construct(array|string $command, string $cwd = null, array $env = null, mixed $input = null, ?float $timeout = 60)只有覆盖被接受的解决方案,所以,没有其他。我需要这样做是因为Behat测试,因为当我运行这些测试时,我会得到错误"Process::__construct():参数#1 ($command)必须是数组类型,字符串给定的“
发布于 2022-04-10 16:58:52
作为一种选择,您可以通过创建自己的类扩展流程组件来更改传入参数:
use Symfony\Component\Process\Process;
class CustomProcess extends Process
{
public function __construct(array|string $command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60)
{
if(is_string($command)){
$command = [$command]; // do something with a string
}
parent::__construct($command, $cwd, $env, $input, $timeout);
}
}然后在控制器中创建类的实例,如下所示:
$command = new CustomProcess('you command');
$command->run();https://stackoverflow.com/questions/71818328
复制相似问题