Download the PHP package byjg/phpthread without Composer
On this page you can find all versions of the php package byjg/phpthread. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download byjg/phpthread
More information about byjg/phpthread
Files in byjg/phpthread
Package phpthread
Short Description Polyfill Implementation of Threads in PHP. This class supports both FORK process and native Threads using ZTS compilation.
License MIT
Informations about the package phpthread
ByJG PHPThread: Simplified Threads and Non-Blocking Code in PHP
ByJG PHPThread simplifies working with threads and non-blocking code in PHP, bridging the gap for a language that was not inherently designed for threading.
Table of Contents
- Overview
- Why Threads in PHP?
- How It Works
- Default PHP (Non-ZTS)
- PHP ZTS (Zend Thread Safety)
- Features
- Limitations
- Installation
Overview
PHPThread is a polyfill library that abstracts threading functionality for PHP, providing a consistent interface regardless of the underlying PHP setup (ZTS or Fork). It empowers developers to implement thread-like behavior and asynchronous processing in PHP applications.
Why Threads in PHP?
PHP is traditionally designed for a request-response cycle, where scripts are executed only in response to client requests and terminate after sending a response. While efficient for web applications, this architecture lacks native threading support for background or concurrent tasks.
With PHPThread, you can overcome these limitations by leveraging:
- Forking (Default PHP) for simulating threading.
- Zend Thread Safety (ZTS) for true multi-threading environments.
How It Works
Default PHP (Non-ZTS)
In standard PHP installations (without ZTS), threading is simulated using the fork
command.
This approach creates a new process by cloning the parent process. While not a true thread,
this method can approximate threading behavior.
Requirements:
PHP ZTS (Zend Thread Safety)
With ZTS-enabled PHP, true multi-threading becomes possible. This setup is ideal for production environments where
robust threading is required. The ZTS version is compiled with the --enable-zts
flag, but it may not be included in
all PHP distributions.
Requirements:
Features
- Thread Management: Simplified thread creation and execution (docs).
- Thread Pools: Efficiently manage and reuse threads for multiple tasks (docs).
- Promises (Experimental): Asynchronous task management with a promise-like API (docs).
Supported Promise Methods:
then()
: Execute a callback on promise resolution.catch()
: Execute a callback on promise rejection.finally()
: Execute a callback after resolution or rejection.Promise::resolve()
: Resolve a promise.Promise::reject()
: Reject a promise.Promise::all()
: Wait for all promises to resolve.Promise::race()
: Wait for the first promise to resolve.
Limitations
Read the full list of limitations.
Installation
Requirements
Non-ZTS (Default PHP)
- PHP ≥8.1
pcntl
extensionshmop
extension
ZTS (Zend Thread Safety)
- PHP ≥8.1 compiled with
--enable-zts
parallel
extensionshmop
extension (for Promises support)
Install via Composer
Dependencies
All versions of phpthread with dependencies
ext-pcntl Version *
ext-posix Version *
php Version >=8.1 <8.4