1. Go to this page and download the library: Download microse/microse-swoole library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
microse / microse-swoole example snippets
// src/app.php
oduleProxyApp;
// Create an abstract class to be used for IDE intellisense:
abstract class AppInstance extends ModuleProxyApp
{
}
// Create the instance amd add type annotation:
/** @var AppInstance */
$app = new ModuleProxyApp("App");
// src/Bootstrap.php
namespace App;
class Bootstrap
{
public function init(): void
{
// ...
}
}
use App\Bootstrap;
abstract class AppInstance extends ModuleProxyApp
{
public Bootstrap $Bootstrap;
}
// src/index.php
e module as a singleton and calling its function directly.
$app->Bootstrap->init();
// src/Services/User.py
namespace App\Services;
class User
{
private $users = [
["firstName" => "David", "lastName" => "Wood"]
]
public function getFullName(string $firstName): string
{
foreach ($this->users as $user) {
if ($user["firstName"] === $firstName) {
return $firstName . " " . $user["lastName"];
}
}
}
}
// src/app.php
use App\Services\User;
abstract class AppInstance extends ModuleProxyApp
{
public Services $Services;
}
abstract class Services
{
public User $User;
}
// src/server.php
global $app;
$server = $app->serve("ws://localhost:4000");
// Register the service, no need to
// client.php
() {
global $app;
$client = $app->connect("ws://localhost:4000");
$client->register($app->Services->User);
// Accessing the instance in local style but actually calling remote.
// Since we're using swoole, this procedure is actually asynchronous.
$fullName = $app->Services->User->getFullName("David");
echo $fullName . "\n"; // David Wood
});
// src/Services/User.php
namespace App\Services;
class User
{
private $friends = [
"David" => [
[ "firstName" => "Albert", "lastName" => "Einstein" ],
[ "firstName" => "Nicola", "lastName" => "Tesla" ],
// ...
],
// ...
];
public function getFriendsOf(string $name): \Generator
{
$friends = @$this->friends[$name];
if ($friends) {
foreach ($friends as $friend) {
yield $friend["firstName"] => $friend["lastName"];
// NOTE: only PHP supports 'yield $key => $value', if this
// function is call from other languages, such as Node.js,
// the '$key' will be ignored.
}
return "These are all friends";
}
}
}
$generator = $app->Services->User->getFriendsOf("David");
foreach ($generator as $firstName => $lastName) {
echo $firstName . " ". $lastName . "\n";
// Albert Einstein
// Nicola tesla
// ...
}
// We can get the return value as well:
$returns = $generator->getReturn(); // These are all friends
use Microse\ModuleProxyApp;
/** @var AppInstance */
$app = new ModuleProxyApp("app", false); // pass the second argument false
go(function () use ($app) {
$client = $app->connect("ws://localhost:4000");
$client->register($app->services->user);
$fullName = $app->services->user->getFullName("David");
echo $fullName . "\n"; // David Wood
});
abstract class AppInstance extends ModuleProxyApp
{
public services $services;
}
abstract class services
{
public user $user;
}
// Use 'interface' works as well since 'user' doesn't contain properties.
interface user
{
abstract function getFullName(string $name): string;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.