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

时间:2013-01-25 01:59:55

标签: codeigniter xampp php

我正在尝试使用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

  

我正在使用Codeigniter 2.3和XAMPP,它有PHP 5.3

2 个答案:

答案 0 :(得分:2)

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

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']();
  }

答案 1 :(得分:2)

检查您的服务器日志,看看是否存在与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);
}

请注意,您不要触摸config.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();

?>