An Actor can spawn any number of actors.
The spawned actors become children actors, and each can spawn their own children actors.
(In this case, the spawning actor becomes the parent.)
This allows actors to have a hierarchical structure.
The ActorSystem acts as the host of the hierarchy,
and the root actor that can be spawned directly under it becomes the top-level actor.
The lifecycle of a child actor depends on the lifecycle of its parent actor.
Child actors can be stopped or restarted at any time,
but if the parent actor stops, its child actors also stop, so they cannot outlive their parent.
When spawning an actor, the root actor is created from root()
, and the child actor is created from context
.
spawn
method is used to create a child actor or root actor.
first parameter is a Phluxor\ActorSystem\Props
object.
Spawn an actor with auto-generated name.
Props is a configuration object for creating actors.
use Phluxor\ActorSystem;
use Phluxor\ActorSystem\Props;
$system = ActorSystem::create();
$spawned = $system->root()->spawn(
Props::fromProducer(
fn() => new YourActor()
)
);
namespace App\ActorSystem;
use App\Command\CreateUser;
use Phluxor\ActorSystem\Context\ContextInterface;
use Phluxor\ActorSystem\Message\ActorInterface;
use Phluxor\ActorSystem\Props;
class YourActor implements ActorInterface
{
public function receive(ContextInterface $context): void
{
if ($msg instanceof CreateUser) {
$context->spawn(
Props::fromProducer(
fn() => new YourChildActor()
)
);
}
}
}
The method for spawning root and child is the same below.
spawnNamed
method is used to create a named actor.
unique name is required as the second parameter.
You can create an actor with a name in advance,
You can send messages to the intended actor and manage the state more easily.
if the name is already in use,
an exception(Phluxor\ActorSystem\Exception\NameExistsException
) is thrown.
namespace App\ActorSystem;
use App\Command\CreateUser;
use Phluxor\ActorSystem\Context\ContextInterface;
use Phluxor\ActorSystem\Message\ActorInterface;
use Phluxor\ActorSystem\Props;
class YourActor implements ActorInterface
{
public function receive(ContextInterface $context): void
{
if ($msg instanceof CreateUser) {
$context->spawnNamed(
Props::fromProducer(
fn() => new YourChildActor()
),
'unique-name'
);
}
}
}
spawnPrefix
method is used to create an actor with a prefix.
The prefix is added to the name of the actor.
namespace App\ActorSystem;
use App\Command\CreateUser;
use Phluxor\ActorSystem\Context\ContextInterface;
use Phluxor\ActorSystem\Message\ActorInterface;
use Phluxor\ActorSystem\Props;
class YourActor implements ActorInterface
{
public function receive(ContextInterface $context): void
{
if ($msg instanceof CreateUser) {
$context->spawnPrefix(
Props::fromProducer(
fn() => new YourChildActor()
),
'prefix-'
);
}
}
}