PSL

PHP Standard Library

A standard library for PHP, inspired by hhvm/hsl. Provides a consistent, centralized, well-typed set of APIs covering async, collections, networking, I/O, cryptography, terminal UI, and more - replacing PHP functions and primitives with safer, async-ready alternatives that error predictably.

or get everything at once

composer require php-standard-library/php-standard-library
use Psl\Type;

$userType = Type\shape([
    'name' => Type\non_empty_string(),
    'age'  => Type\positive_int(),
    'tags' => Type\vec(Type\string()),
]);

$user = $userType->coerce($untrustedInput);
// array{name: non-empty-string,
//   age: positive-int, tags: list<string>}

Type-Safe from Input to Output

Validate and coerce untrusted data with composable type combinators. Shapes, unions, optionals: all with zero reflection overhead.

use Psl\Async;
use Psl\IO;

Async\main(static function(): int {
    [$a, $b, $c] = Async\concurrently([
        static fn() => Async\sleep(0.1),
        static fn() => Async\sleep(0.2),
        static fn() => Async\sleep(0.1),
    ]);

    IO\write_error_line('Done in ~0.2s, not 0.4s');

    return 0;
});

Async Without the Ceremony

Run concurrent operations with a single function call. Structured concurrency built on fibers. No promises, no callbacks.

use Psl\Vec;
use Psl\Dict;
use Psl\Str;

$names = ['alice', 'bob', 'charlie', 'dave'];

Vec\map($names, Str\uppercase(...));
// ['ALICE', 'BOB', 'CHARLIE', 'DAVE']

Vec\filter($names, fn($n) => Str\length($n) > 3);
// ['alice', 'charlie', 'dave']

Dict\pull($names, Str\uppercase(...), fn($n) => $n);
// {alice: 'ALICE', bob: 'BOB', ...}

Collections That Make Sense

Map, filter, sort, and reshape arrays with pure functions. Separate return types for lists and dicts. No more array key confusion.

use Psl\Async;
use Psl\TCP;
use Psl\IO;

Async\main(static function(): int {
    $server = TCP\listen('127.0.0.1', 8080);
    IO\write_error_line('Listening on :8080');

    while (true) {
        $conn = $server->accept();
        Async\run(static function() use ($conn) {
            $conn->writeAll("Hello!\n");
            $conn->close();
        })->ignore();
    }
});

TCP Server in 10 Lines

Production-ready networking primitives. TCP, TLS, UDP, Unix sockets: all async, all composable.