Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling a function from inside $pool->add? #168

Closed
carys-cc opened this issue Nov 3, 2021 · 2 comments
Closed

Calling a function from inside $pool->add? #168

carys-cc opened this issue Nov 3, 2021 · 2 comments

Comments

@carys-cc
Copy link

carys-cc commented Nov 3, 2021

This might be a really silly question. I'm kind of new to PHP.

I have a function called test() that just takes the object $thing and does addition.
However when I run this, nothing is output.

Seems like functions are not available inside the pool->add function?

use Spatie\Async\Pool;

$pool = Pool::create();

foreach ($things as $thing) {
    $thing->a = 1;
    $thing->b = 2;

    $pool->add(function () use ($thing) {
        // Do a thing
        return test($thing);           // ***** is this even being called?
    })->then(function ($output) {
        // Handle success
 
        echo $output;           // ***** nothing happens here
    })->catch(function (Throwable $exception) {
        // Handle exception
    });
}

$pool->wait();


//     *** just a simple addition function
function test($thing) {
  return $thing->a + $thing->b;
}
@Stevemoretz
Copy link
Contributor

No functions are not magically moved around, you need to load all your required functions via a custom autoloader, then it will work.

@spatie-bot
Copy link

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants