如何修复php错误“无法通过引用传递参数1”?

时间:2019-11-12 06:44:26

标签: php drupal

我知道在stackoverflow上,这个错误有很多类似的问题,但是没有一个能解决我的问题。我在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);

1 个答案:

答案 0 :(得分:2)

错误提示,FileSystem::prepareDirectory通过引用采用第一个参数:

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

因此,如果您将PHP传递给constant,则会抛出错误:

  

常量是简单值的标识符(名称)。作为名字   提示,在脚本执行期间该值不能更改

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

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