Magento – Magento 2.3 PHP Parse error unexpected ‘?’ in write.php

cronerrormagento2.3php-7.2ssh

I want to Install cron with putty SSH. I use php bin/magento cron:install [--force] and get this error:

PHP Parse error: syntax error, unexpected '?', expecting variable
(T_VARIABLE) in
/www/htdocs/xxx/xxx/xxx/vendor/magento/framework/Filesystem/Directory/Write.php
on line 35

Server run with PHP Version 7.2.11-nmm1. What can I do? Where I can begin to search for it?

Best Answer

It seems your php version does not parse correctly Nullable types (such as that ?PathValidatorInterface), which were introduced in php7.1 http://php.net/manual/en/migration71.new-features.php

public function __construct(
    \Magento\Framework\Filesystem\File\WriteFactory $fileFactory,
    \Magento\Framework\Filesystem\DriverInterface $driver,
    $path,
    $createPermissions = null,
    ?PathValidatorInterface $pathValidator = null
) {
    parent::__construct($fileFactory, $driver, $path, $pathValidator);
    if (null !== $createPermissions) {
        $this->permissions = $createPermissions;
    }
}

Double check your php version in command line, as it does not seem to be 7.2

It is not rare to have more than 1 php version installed, for instance https://stackoverflow.com/a/50591861/1073040