【问题标题】:Cannot pass the apiClient instance to another class with Codeigniter无法使用 Codeigniter 将 apiClient 实例传递给另一个类
【发布时间】:2012-03-01 23:41:00
【问题描述】:

我正在尝试加载 PHP 中的 Google 日历 API,但首先要加载 API,我需要将 apiClient 类实例传递给另一个类,因为我没有成功m 使用 Codeigniter 加载apiClient 库,但加载第二个类失败。

我做了什么:

$this->load->library('google/apiClient', NULL, 'apiClient');

echo gettype($this->apiClient); // object

// This class needs the `apiClient` class to be passed to this, but fails
$this->load->library('google/contrib/apiCalendarService', $this->apiClient);

抛出的错误是这样的:

传递给 apiCalendarService::__construct() 的参数 1 必须是 apiClient 的实例,没有给出

但是,如果我这样做:

$this->load->library('google/contrib/apiCalendarService', array($this->apiClient));

它会抛出这个错误(在数组中传递$this->apiClient):

传递给 apiCalendarService::__construct() 的参数 1 必须是 apiClient 的实例,给定数组

这里有什么问题?如何专门将apiClient 传递给apiCalendarService 类?

【问题讨论】:

    标签: php codeigniter google-calendar-api


    【解决方案1】:

    这里的问题是 CodeIgniter 需要一个数组作为传递给所有库的第一个参数,因此并非所有类默认都适合作为 CI 库(尤其是那些需要构造函数参数的类)。

    我在这里看到两个选项:

    1. 以“旧方式”加载该死的东西:include 类并实例化new apiCalendarService($this->apiClient)
    2. 创建一个包装类来“弥合”两个不兼容的框架之间的差距

    顺便说一下,选项 #1 是两行代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-13
      • 2023-01-25
      • 2016-08-27
      • 1970-01-01
      • 2015-03-22
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多