array_merge() [function.array-merge]:参数 #1 不是数组


array_merge() [function.array-merge]: Argument #1 is not an array

我正在尝试使用Google-API-PHP-Client,其基类抛出以下错误:

Severity: Warning
Message: array_merge() [function.array-merge]: Argument #1 is not an array
Filename: libraries/Google_Client.php
Line Number: 107

像 107 这样的代码是这样的:

public function __construct($config = array()) {
    global $apiConfig;
    $apiConfig = array_merge($apiConfig, $config);
    self::$cache = new $apiConfig['cacheClass']();
    self::$auth = new $apiConfig['authClass']();
    self::$io = new $apiConfig['ioClass']();
  }

我知道global $apiConfig没有初始化为数组,这就是array_merge抛出错误的原因。但是当我将其更改为global $apiConfig = array();时,又出现了一个错误Parse error: syntax error, unexpected '=', expecting ',' or ';' in C:'Softwares'xampp'htdocs'testsaav'application'libraries'Google_Client.php on line 106

我正在使用带有 PHP 5.3 的 XAMPP 的 Codeigniter 2.3

在函数中初始化数组(如有必要)

public function __construct($config = array()) {
    global $apiConfig;
    $apiConfig = (isset($apiConfig) && is_array($apiConfig)) ? $apiConfig : array(); // initialize if necessary
    $apiConfig = array_merge($apiConfig, $config);
    self::$cache = new $apiConfig['cacheClass']();
    self::$auth = new $apiConfig['authClass']();
    self::$io = new $apiConfig['ioClass']();
  }

检查您的服务器日志,查看是否存在与Google_Client.php中的require_once('config.php')相关的错误(如果未找到该文件,则脚本应该已停止)。

执行 require_once('Google_Client.php') 时,将从该文件执行以下代码。满足要求后,脚本应该可以看到$apiConfig。

// hack around with the include paths a bit so the library 'just works'
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
require_once "config.php";
// If a local configuration file is found, merge it's values with the default configuration
if (file_exists(dirname(__FILE__)  . '/local_config.php')) {
  $defaultConfig = $apiConfig;
  require_once (dirname(__FILE__)  . '/local_config.php');
  $apiConfig = array_merge($defaultConfig, $apiConfig);
}

请注意,您不会触摸配置.php。如果需要覆盖其中的任何内容,请创建local_config.php。

在 PHP 5.3 的系统中,我使用了这个脚本。如下所示的脚本不会引发任何错误。取消设置$apiConfig会复制错误。

<?php
require_once('src/Google_Client.php');
print_r($apiConfig);
// uncommenting the next line replicates issue.
//unset($apiConfig);
$api = new Google_Client();
?>