Skip to content

Commit

Permalink
new example: how to use different types of callbacks when creating co…
Browse files Browse the repository at this point in the history
…routines

Signed-off-by: Demin Yin <deminy@deminy.net>
  • Loading branch information
deminy committed Dec 14, 2023
1 parent 5787491 commit 0f65c6c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ docker compose exec -ti client bash # Get a Bash shell in the client container.
* [This example](https://github.com/deminy/swoole-by-examples/blob/master/examples/io/blocking-vs-non-blocking.php) shows how the _return_ statement is treated differently in Swoole. As you can see in the example, a function call could return a value back first before finishing its execution.
* coroutines
* create coroutines
* use different callbacks to create coroutines
* [use different types of callbacks when creating coroutines](https://github.com/deminy/swoole-by-examples/blob/master/examples/csp/coroutines/creation-2.php)
* [use different functions/methods to create coroutines](https://github.com/deminy/swoole-by-examples/blob/master/examples/csp/coroutines/creation-1.php)
* enable coroutines
* [coroutines in a for loop](https://github.com/deminy/swoole-by-examples/blob/master/examples/csp/coroutines/for.php)
Expand Down
68 changes: 68 additions & 0 deletions examples/csp/coroutines/creation-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

/**
* This example shows how to use different types of callbacks when creating coroutines.
*
* How to run this script:
* docker compose exec -t client bash -c "./csp/coroutines/creation-2.php"
*
* @see https://www.php.net/manual/en/language.types.callable.php Callbacks / Callables
*/

use Swoole\Coroutine;

use function Swoole\Coroutine\run;

// Define a callback function.
function callbackFunction(int $i): void
{
echo $i;
}

// Define a class with both static and non-static callback methods included.
class callbackClass
{
public function callbackMethod(int $i): void
{
echo $i;
}

public static function staticCallbackMethod(int $i): void
{
echo $i;
}
}

// An invokable class.
class InvokableClass
{
public function __invoke(int $i): void
{
echo $i;
}
}

run(function () {
// Type 1: A simple callback function defined directly within the code block.
Coroutine::create(function () {echo 1; });

// Type 2: A simple callback function defined somewhere previously.
Coroutine::create('callbackFunction', 2);

// Type 3: Use object method call as callback.
Coroutine::create([new callbackClass(), 'callbackMethod'], 3);

// Type 4: Use static class method call as callback.
Coroutine::create(['callbackClass', 'staticCallbackMethod'], 4);

// Type 5: Use static class method call as callback.
Coroutine::create('callbackClass::staticCallbackMethod', 5);

// Type 6: Objects implementing __invoke can be used as callbacks.
Coroutine::create(new InvokableClass(), 6);
});

echo PHP_EOL;

0 comments on commit 0f65c6c

Please sign in to comment.