在 Windows 10 中执行命令行,报错:Call to undefined function App\Console\Commands\posix_getpid()

1、在 Windows 10 中执行命令行,报错:Call to undefined function App\Console\Commands\posix_getpid()。如图1

图1

PS E:\wwwroot\msi_main> php artisan migrate

In ExportRecords.php line 65:

  Call to undefined function App\Console\Commands\posix_getpid()


2、参考网址:https://www.php.net/manual/zh/intro.posix.php 。此扩展在 Windows 平台上不可用。您也可以尝试使用 getmypid() 代替。如图2

图2

3、posix_getpid — 返回当前进程 id。决定替换为:getmypid — 获取 PHP 进程的 ID。参考网址:https://www.php.net/getmypid。

4、编辑 /app/Console/Commands/ExportRecords.php 。将 posix_getpid 替换为 getmypid。

    private function setLogPrefix() {
        // $pid = posix_getpid();
  $pid = getmypid();
        $ppid = posix_getppid();  //获取父进程id
        $this->log_prefix = "CommandExportRecords : pid:{$pid} : ppid:{$ppid} :";
        return $this;
    }

5、继续执行命令行,报错:Call to undefined function App\Console\Commands\posix_getppid()。

PS E:\wwwroot\msi_main> php artisan migrate

In ExportRecords.php line 67:

  Call to undefined function App\Console\Commands\posix_getppid()


6、posix_getppid 在 Windows 上不起作用。这是一个替代方案。参考网址:https://www.php.net/manual/zh/function.posix-getppid 。判断是否是 Windows 系统,如果是,就使用替代方案。如图3

图3

    private function setLogPrefix() {
  if(strncasecmp(PHP_OS, "win", 3) == 0) {
   $pid = getmypid();  // child process ID
   $ppid = shell_exec("wmic process where (processid=$pid) get parentprocessid");
   $ppid = explode("\n", $ppid);
   $ppid = intval($ppid[1]);
  } else {
   $pid = posix_getpid();
   $ppid = posix_getppid();  //获取父进程id
  }
        $this->log_prefix = "CommandExportRecords : pid:{$pid} : ppid:{$ppid} :";
        return $this;
    }

7、继续执行命令行,未再报错。如图4

图4

PS E:\wwwroot\msi_main> php artisan migrate
Migration table created successfully.
永夜