在我的控制器中使用外部类


Using external class in my Controller

我正在CodeIgniter中进行我的第一个项目,我想知道如何在控制器中使用库中的类。

librarys/Twitterclass.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Twetterclass {
public function __construct($hashtag, $tweet_id)
{
    require_once('TwitterAPIExchange.php');
    //There is my working code
    //I want to make use of this $n in my Controller
    return $n;
}
}
/* End of file Twetterclass.php */

我的控制器:

    public function microtweets()
    {
        $params = array('hashtag' => 'somehashtag', 'tweet_id' => '673899616799191040');
        $data['count_tweets'] = $this->load->library('Twetterclass', $params);
        $this->load->view('tweets', $data);
    }

我想在我的控制器中使用他的扩展类,并在那里处理这个$n值,或者例如在我的视图中显示它

我得到一些错误:

遇到PHP错误

严重性:警告

消息:Twtterclass::__construct()缺少参数2,在中调用/home/pillal/kg7dad5/home/kg7dad5/域/badzlepszy.pl/public_html/conmonitor/system/core/Loader.php在线1246上,并定义

文件名:librarys/Tweterclass.php

线路编号:5

回溯:

文件:/application/librarys/Tweterclass.php行:5函数:_错误处理程序

文件:/application/controllers/Cointweet.php行:24函数:库

文件:/public_html/coninmonitor/index.php行:292函数:需要一次

首先尝试调试在构造函数中传递的参数

public function __construct($hashtag, $tweet_id)
{
   echo $hashtag;
   echo $tweet_id;
   die; 
}

根据您的代码,像这样更改,

public function __construct($arr)
{
   echo $arr['hashtag'];
   echo $arr['tweet_id'];
   exit; 
}

因为您传递的是1个数组,所以使用数组索引访问库。