Unlock Modern PHP Development with the PSL Standard Library

The article introduces azjezz/psl, a PHP Standard Library inspired by hhvm/hsl, explains its goals, features, Composer installation, integration with Psalm and PHPStan, and provides detailed code examples for async tasks, password handling, Unix socket communication, and TCP parallel networking.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Unlock Modern PHP Development with the PSL Standard Library

Overview

azjezz/psl is a PHP Standard Library that offers a consistent, centralized, type‑safe API collection inspired by hhvm/hsl, aiming to simplify development, improve maintainability and portability of PHP code.

Features

Goal : Provide PHP developers with a standard, centralized, type‑safe API.

Inspiration : Based on hhvm/hsl, the standard library for HHVM.

Functionality : Async programming (Async namespace), TCP connection management (TCP), I/O (IO), shell command execution (Shell), string handling (Str), etc.

Installation : Available via Composer.

Integration : Works with static analysis tools Psalm and PHPStan.

Installation

Install libsodium: sudo /usr/local/php-8.3.1/bin/pecl install -f libsodium Install the library:

/usr/local/php-8.3.1/bin/php composer require azjezz/psl

Cooperative Multitasking

Cooperative Multitasking : A multitasking model where each running program voluntarily yields control, allowing the OS to schedule the next task. If a program fails to yield, it can block the whole system.
Async component is built on RevoltPHP, compatible with Amphp and other event‑loop libraries.
RevoltPHP: https://github.com/revoltphp/event-loop

Simple Usage

Async\main(static function(): int {
    $watcher = Async\Scheduler::onSignal(SIGINT, function(): never {
        IO\write_error_line('SIGINT received, stopping...');
        exit(0);
    });
    Async\Scheduler::unreference($watcher);
    IO\write_line('Hello, World!');
    [$version, $connection] = Async\concurrently([
        static fn() => Shell\execute('php', ['-v']),
        static fn() => TCP\connect('172.30.237.24', 2023),
    ]);
    $messages = Str\split($version, "
");
    IO\write_line('php, -v!' . json_encode($messages));
    foreach ($messages as $message) {
        $connection->writeAll($message);
    }
    $connection->close();
    return 0;
});

Output:

Hello, World!
php, -v!["PHP 8.3.1 (cli) ...","Copyright ...","Zend Engine v4.3.1 ...",""]

Password Component

<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use function Psl\Password\hash;
use function Psl\Password\verify;
use function Psl\Password\get_information;
use function Psl\Password
eeds_rehash;

$password = 'Resty2024';
$hash = hash($password);
echo "[x] 加密Hash:" . $hash . PHP_EOL;

$check = verify($password, $hash);
echo "[x] 验证结果:" . $check . PHP_EOL;

$info = get_information($hash);
echo "[x] 获取Hash:" . json_encode($info) . PHP_EOL;

needs_rehash($hash);

Execution output:

[x] 加密Hash:$2y$10$... 
[x] 验证结果:1 
[x] 获取Hash:{"algorithm":"bcrypt","options":{"cost":10}}

Unix Socket Example

<?php
declare(strict_types=1);
require __DIR__.'/vendor/autoload.php';
use Psl\Async;
use Psl\Filesystem;
use Psl\IO;
use Psl\Str;
use Psl\Unix;

Async\main(static function(): int {
    if (PHP_OS_FAMILY === 'Windows') {
        IO\write_error_line('This example does not support Windows.');
        return 0;
    }
    $file = Filesystem\create_temporary_file(prefix: 'psl-examples') . ".sock";

    Async\concurrently([
        'server' => static function() use ($file): void {
            $server = Unix\Server::create($file);
            IO\write_error_line('< server is listening.');
            $connection = $server->nextConnection();
            IO\write_error_line('< connection received.');
            $request = $connection->read();
            IO\write_error_line('< received request: "%s".', $request);
            $connection->writeAll(Str\reverse($request));
            $connection->close();
            $server->close();
            IO\write_error_line('< server stopped');
        },
        'client' => static function() use ($file): void {
            $client = Unix\connect($file);
            IO\write_error_line('> client connected.');
            $client->writeAll('Hello, World!');
            $response = $client->readAll();
            IO\write_error_line('> received response: "%s".', $response);
            $client->close();
        },
    ]);
    return 0;
});

The sample output shows the server listening, receiving the request, sending the reversed string, and both sides closing.

TCP Parallel Example

<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Psl\Async;
use Psl\IO;
use Psl\Str;
use Psl\TCP;

Async\main(static function(): int {
    Async\concurrently([
        'server' => static function(): void {
            $server = TCP\Server::create('localhost', 20248);
            IO\write_error_line('< server is listening.');
            $connection = $server->nextConnection();
            $request = $connection->read();
            IO\write_error_line('< received request: "%s".', $request);
            $connection->writeAll(Str\reverse($request));
            $connection->close();
            $server->close();
            IO\write_error_line('< server stopped.');
        },
        'client' => static function(): void {
            $client = TCP\connect('localhost', 20248);
            $client->writeAll('Hello, World!');
            $response = $client->readAll();
            IO\write_error_line('> received response: "%s".', $response);
            $client->close();
        },
    ]);
    return 0;
});
Listening port: 20248
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPComposerStandard LibraryPSL
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.