Search code examples
phpzend-frameworkstomp

Fatal error: Uncaught Error: Class 'Stomp' not found


I've downloaded the library available in: https://github.com/dejanb/stomp-php

and then implemented the following code:

<?php

   use FuseSource\Stomp\Stomp;
   (...)
$data=array($data1,$data2, $data3, $data4);
$json = json_encode($data, true);


$user = getenv("ACTIVEMQ_USER"); 
if( !$user ) $user = "admin";

$password = getenv("ACTIVEMQ_PASSWORD");
if( !$password ) $password = "password";

$destination  = '/topic/event';
$messages = 10000;
$size = 256;

$DATA = "calls";
$body = $data;
for($i=0; $i< $size; $i++) {
  $body .= $DATA[ $i % 26];
}

try {
  $url = 'tcp://localhost:61613';
  $con = new Stomp($url, $user, $password);

  for($i=0; $i< $messages; $i++) {
    $con->send($destination, $body);
    if( $i%1000 == 0 ) {
      echo "Sent ".$i." messages\n";
    }
  }

  $stomp->send($destination, "SHUTDOWN");

} catch(StompException $e) {
  echo $e->getMessage();
}
}

and I get this error:

Fatal error: Uncaught Error: Class 'Stomp' not found in /opt/lampp/htdocs/skeleton-application/test.php:80

UPDATE: I reinstalled using composer as suggested at https://github.com/stomp-php/stomp-php.

I required the autoloader using

<?php
require __DIR__ . '/../vendor/autoload.php';

and added the following imports

use Stomp\Client;
use Stomp\StatefulStomp;
use Stomp\Network\Connection;
use Stomp\Transport\Message;

Still same error... Do I need to do anything special with my activemq broker or something? I ran it through the console... but still nothing


Solution

  • Apart from the confusion which package to use, our code has the following problems:

    • you haven't imported the classes you are using, and thus, PHP cannot find the classes within the root namespace
    • you are passing in parameters to constructors and other methods that aren't used, or have the wrong type
    • you apparently have undefined variables (for example, $stomp is undefined, or it's missing from the code example)

    I suggest tackling these problems one by one.

    Using fusesource/stomp-php

    Import the classes you are using

    Adjust your list of imports (these are the use statements) to include the classes you are referencing in the code example:

    use FuseSource\Stomp\Exception\StompException;
    use FuseSource\Stomp\Stomp;
    

    If there are more classes from the package that your code example omits, include them in your imports as well.

    Remove unnecessary parameters

    When you create an instance of FuseSource\Stomp\Stomp, you are passing in parameters the constructor doesn't use.

    Change

    $con = new Stomp($url, $user, $password);
    

    to

    $con = new Stomp($url);
    

    Undefined variables

    You have a line of code where you reference an undefined variable $stomp, where you probably intend to use $con instead.

    Change

    $stomp->send($destination, "SHUTDOWN");
    

    to

    $con->send($destination, new Message("SHUTDOWN"));
    

    Using stomp-php/stomp-php

    Import the classes you are using

    Adjust your list of imports (these are the use statements) to include the classes you are referencing in the code example:

    use Stomp\Client;
    use Stomp\Exception\StompException;
    use Stomp\StatefulStomp;
    use Stomp\Transport\Message;
    

    If there are more classes from the package that your code example omits, include them in your imports as well.

    Remove unnecessary parameters

    The constructor of Stomp\Stomp\StatefulStomp has a different signature than that of FuseSource\Stomp.

    Change

    $con = new Stomp($url, $user, $password);
    

    to

    $con = new StatefulStomp(new Client($url));
    

    Adjust parameters to the required type

    The signature of send() has changed.

    Change

    $con->send($destination, $body);
    

    to

    $con->send($destination, new Message($body));        
    

    Undefined variables

    You have a line of code where you reference an undefined variable $stomp, where you probably intend to use $con instead.

    Change

    $stomp->send($destination, "SHUTDOWN");
    

    to

    $con->send($destination, new Message("SHUTDOWN"));
    

    For reference, see: