0

i have an error in my code. I want to call the get_mail() function in the index function. This code is rabbitmq with phpcodeigniter consumer code, error message is:

Type: Error

Message: Class 'get_mail' not found

Filename: C:\xampp\htdocs\mail\application\controllers\Consumer.php

Line Number: 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 */


?>

this is error message from php: Type: Error

Message: Using $this when not in object context

Filename: C:\xampp\htdocs\mail\application\controllers\Consumer.php

Line Number: 45

2 Answers2

0

As i can see, you need to create a separate object of the class to use get_email. Problem is: process_message is execute by Queue And The internal Queue class doesn't have get_email, that;s why you are getting the error - not found.

Possible Solution: create a Separate class for email and create the object whenever it is need, even in your method.

0

PHP does not support nested functions, so when you write code like this...

class X {
    public function foo() {
        function bar() {
            echo "hello world";
            var_dump($this); // this will give an error
        }

        something_needing_a_callback('bar');
    }
}

...you are actually just declaring a global function called bar, and it has no relationship to class X; it is as though you'd written this (except that the function doesn't get declared until you run 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
}

What you are probably looking for is closures, AKA anonymous functions, which can "capture" or "bind" variables from their surrounding scope, and automatically bind $this; so you could write this:

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

Alternatively, you can write a named method on the class and use that as the callback using the syntax for referencing callables, but note that it needs to be public, since the function that will eventually call it is not inside the class:

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
    }
}

Hopefully you can work out how to apply that to your actual code.

IMSoP
  • 89,526
  • 13
  • 117
  • 169