php - 类型:错误消息:不在对象上下文中使用 $this
问题描述
我的代码有错误。我想在index函数中调用get_mail()函数。此代码是带有 phpcodeigniter 消费者代码的 rabbitmq,错误消息是:
类型:错误
消息:找不到类“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
解决方案
PHP 不支持嵌套函数,所以当你编写这样的代码时...
class X {
public function foo() {
function bar() {
echo "hello world";
var_dump($this); // this will give an error
}
something_needing_a_callback('bar');
}
}
...您实际上只是声明了一个名为 的全局函数bar
,它与 class 无关X
;就好像你已经写了这个(除了函数在你运行之前不会被声明foo
):
class X {
public function foo() {
something_needing_a_callback('bar');
}
}
function bar() {
echo "hello world";
var_dump($this); // $this has no meaning here, we are not inside a class
}
您可能正在寻找的是闭包,AKA 匿名函数,它可以从其周围范围“捕获”或“绑定”变量,并自动绑定$this
;所以你可以这样写:
class X {
public function foo() {
$myBarFunction = function() {
echo "hello world";
var_dump($this); // works :)
}
something_needing_a_callback($myBarFunction);
}
}
或者,您可以在类上编写一个命名方法,并使用引用 callables 的语法将其用作回调,但请注意它必须是公共的,因为最终调用它的函数不在类内部:
class X {
public function foo() {
something_needing_a_callback([$this, 'bar']);
}
public function bar() {
echo "hello world";
var_dump($this); // OK, because just an ordinary method
}
}
希望你能弄清楚如何将它应用到你的实际代码中。
推荐阅读
- python - 从网络路径移动特定文件(有例外)
- c++ - C++ 内联初始化成员变量的正确方法
- c# - C# BlockingCollection 处置方法
- python - WTF Form SelectField ValueError:解压的值太多
- python - 在 matplotlib 中使用 Debye+Einstein 模型进行比热拟合
- c# - 如何将 UInt 和其他类型转换为 String Hex 以将数据发送到服务器?C#
- r - 如何在 R 中将整数 123033 转换为时间 12:30:33 格式
- laravel - 我想让我的项目在其他浏览器中运行
- javascript - How to edit chart data using office.js within Microsoft Word
- php - 如何修复 CURL 引号 strpos() 问题?