【问题标题】:Type: Error Message: Using $this when not in object context类型:错误消息:不在对象上下文中使用 $this
【发布时间】: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


【解决方案1】:

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全局 函数,它与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
}

您可能正在寻找的是closures,AKA 匿名函数,它可以从其周围范围“捕获”或“绑定”变量,并自动绑定$this;所以你可以这样写:

class X {
     public function foo() {
          $myBarFunction = function() {
                echo "hello world";
                var_dump($this); // works :)
          }
          something_needing_a_callback($myBarFunction);
     }
}

或者,您可以在类上编写一个命名方法并将其用作回调using the syntax for referencing 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
    }
}

希望您能弄清楚如何将其应用到您的实际代码中。

【讨论】:

    【解决方案2】:

    如我所见,您需要创建一个单独的类对象才能使用 get_email。 问题是: process_message 由 Queue 执行并且内部 Queue 类没有 get_email,这就是您收到错误的原因 - 未找到。

    可能的解决方案:为电子邮件创建一个单独的类,并在需要时创建对象,即使在您的方法中也是如此。

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 2016-08-12
      • 1970-01-01
      • 2013-10-16
      • 2010-12-11
      • 1970-01-01
      相关资源
      最近更新 更多