如何修复 php 错误 "Cannot pass parameter 1 by reference"?

How to fix php error "Cannot pass parameter 1 by reference"?

我知道在 Whosebug 上有很多与此错误类似的问题,但是 none 解决了我的问题。我在 high_contrast.module 文件中有一个函数:

function high_contrast_install() {
  $background = \Drupal::config('high_contrast.settings')->get('colors_background');
  $text = \Drupal::config('high_contrast.settings')->get('colors_text');
  $hyperlinks = \Drupal::config('high_contrast.settings')->get('colors_hyperlinks');

  \Drupal::service('file_system')->prepareDirectory(HIGH_CONTRAST_CSS_FOLDER, FileSystemInterface::CREATE_DIRECTORY); // LINE 17!!!
  $css = _high_contrast_build_css();
  file_save_data($css, HIGH_CONTRAST_CSS_LOCATION, FileSystemInterface::EXISTS_REPLACE);
}

当我想安装模块时,出现错误:

Error: Cannot pass parameter 1 by reference in high_contrast_install() (line 17 of modules\high_contrast\high_contrast.install).

我该如何解决? :(

第 17 行是:

  \Drupal::service('file_system')->prepareDirectory(HIGH_CONTRAST_CSS_FOLDER, FileSystemInterface::CREATE_DIRECTORY);

如错误所示,FileSystem::prepareDirectory 通过引用获取其第一个参数:

public function prepareDirectory(&$directory, $options = self::MODIFY_PERMISSIONS) {
  # $directory being assigned new value
}

所以 PHP 如果你传递一个 constant 会抛出一个错误:

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script

您需要在函数调用之前将常量值分配给变量:

$dir = HIGH_CONTRAST_CSS_FOLDER;
\Drupal::service('file_system')->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY);