Actor Model¶
The Actor Model, introduced by Carl Hewitt in 1973, is a mathematical model of concurrent computation. Its fundamental unit is the actor: a lightweight, isolated entity with its own private state and a mailbox. Actors never share memory. All communication happens exclusively through asynchronous message passing.
When an actor receives a message it may: + Create new actors. + Send messages to actors whose addresses it knows. + Designate the behavior to use for the next message it receives (changing its own state).
Because no memory is shared, race conditions and the need for explicit locks are eliminated by design.
PHP is single-threaded in its classic request/response model, so the Actor Model is not natively available in the language. However, several frameworks and extensions bring actor-like concurrency to PHP:
+ ReactPHP and AMPHP implement event loops with async message passing via promises and fibers.
+ Swoole / OpenSwoole provide coroutines and channels that enable actor-style communication.
+ The parallel extension (PHP 7.2+) gives true multi-threaded execution with channels for inter-thread messaging.
+ Libraries such as Phluxor and Thespian implement explicit actor runtimes on top of these primitives.
<?php
// Conceptual actor-style pattern using ReactPHP EventLoop
// Each coroutine owns private state and communicates via channels (parallel ext)
use parallel\{Runtime, Channel};
$channel = new Channel();
$actor = new Runtime();
$actor->run(static function (Channel $inbox): void {
while ($message = $inbox->recv()) {
echo "Actor received: {$message}\n";
}
}, [$channel]);
$channel->send('hello');
$channel->send('world');
$channel->close();
?>
See also ReactPHP, AMPHP, Swoole and parallel extension.
Related : Concurrency, Message Queue, Fibers, Coroutine, Asynchronous, Parallel, Race Condition, Shared Memory, Immutable, Software Transactional Memory (STM)