【发布时间】:2021-02-09 13:25:34
【问题描述】:
我的代码有错误。我想在 index 函数中调用 get_mail() 函数。这段代码是rabbitmq和phpcodeigniter消费者代码,错误信息是:
类型:错误
消息:找不到类“get_mail”
文件名:C:\xampp\htdocs\mail\application\controllers\Consumer.php
行号:45
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH.'../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
class Consumer extends CI_Controller {
public function index(){
$host = "secret";
$port = 5672;
$user = "secret";
$pass = "secret";
$vhost = "secret";
$exchange = 'router';
$queue = 'mail';
$consumerTag = 'consumer';
$connection = new AMQPStreamConnection($host, $port, $user, $pass, $vhost);
$channel = $connection->channel();
$channel->queue_declare($queue, false, true, false, false);
$channel->exchange_declare($exchange, AMQPExchangeType::DIRECT, false, true, false);
$channel->queue_bind($queue, $exchange);
function process_message($message)
{
$pesan = json_decode($message->body);
$isi = $pesan->email;
$this->get_mail();
$message->ack();
}
$channel->basic_consume($queue, $consumerTag, false, false, false, false, 'process_message');
function shutdown($channel, $connection)
{
$channel->close();
$connection->close();
}
register_shutdown_function('shutdown', $channel, $connection);
while ($channel ->is_consuming()) {
$channel->wait();
}
}
public function get_mail($isi){
//this is function for send mail in codeigniter
}
// Loop as long as the channel has callbacks registered
}
/* End of file Consumer.php */
?>
这是来自 php 的错误消息: 类型:错误
消息:不在对象上下文中使用 $this
文件名:C:\xampp\htdocs\mail\application\controllers\Consumer.php
行号:45
【问题讨论】:
-
$this 是什么意思...
-
您对此有何疑问?为什么要把函数放入函数中?
-
为什么你把一些函数放在索引函数上?您将需要使用函数“get_mail”创建一个新类...所以您将调用这个类...
标签: php rabbitmq codeigniter-3 message-queue