3

I am trying to call a user-defined function using Worker\enqueueCallable.
I have put the function in a separate file and used composer to autoload it.

And I am getting the error :

Fatal error: Uncaught TypeError: Argument 1 passed to Amp\Parallel\Worker\enqueueCallable() must be callable, string given"

My folder structure

Project Folder/
    app/
       funcs.php
       caller.php
    main.php
    composer.json

Used namespace app on top in both caller.php and funcs.php

And then in composer.json used the following snippet and used composer dumpautoload -o

{
    "autoload": {
        "psr-4": {
            "app\\": "app/"
        },
        "files": [ "app/funcs.php" ]
    },
    "require": {
        "amphp/parallel": "^1.3",
        "amphp/parallel-functions": "^0.1.3"
    }

} 

From main.php I am calling caller.php which calls a function defined in funcs.php

funcs.php

namespace app;
function f1($param1){
  #Do some stuff 
}

caller.php

namespace app;

use Amp\Parallel\Worker;
use Amp\Promise;

class cls
{
    function __construct()
    {

    }

    function Invoker()
    {
        // If I call the function, its working.
        f1($param1);
        // When I am using its not working.
        $promises[] = Worker\enqueueCallable('f1', $param1);  
    }

}

main.php

require __DIR__ . '\vendor\autoload.php';
use app\cls;

$obj = new cls();
$obj->Invoker();

I am using windows-10, WAMP server, with PHP version 7.3.5.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • 3
    The context in which that callable is going to be evaluated has zero information about what namespace or imports are declared . Use the fully-qualified function name, eg: `app\f1` instead of just `app` – Sammitch Mar 11 '20 at 21:47
  • I'd add to @Sammitch comment that your direct call to the function only works because `caller.php` is in the same namespace as `funcs.php` and because of this you can omit the namespace of the function because it will be implicitly using the same namespace. When in the context of your amphp class, it then need the fully qualified name of the function (so including the namespace) – β.εηοιτ.βε Mar 11 '20 at 21:54
  • Thanks, @Sammitch and β.εηοιτ.βε for helping me understand the issue. It's working after using fully qualified function name **app\f1**. – Vineel Kurma Mar 11 '20 at 22:39

0 Answers0