PHPackages                             mjohann/zynq - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mjohann/zynq

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mjohann/zynq
============

Zynq is a PHP library that brings JavaScript-like asynchronous features to PHP, including support for timers, promises, async/await behavior, and multi-threaded execution via RPC.

v1.1.0(1y ago)915MITPHPPHP ^8.0

Since May 1Pushed 1y ago2 watchersCompare

[ Source](https://github.com/matheusjohannaraujo/zynq)[ Packagist](https://packagist.org/packages/mjohann/zynq)[ RSS](/packages/mjohann-zynq/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

[Zynq](https://github.com/matheusjohannaraujo/zynq)
===================================================

[](#zynq)

**Zynq** is a PHP library that brings JavaScript-like asynchronous features to PHP, including support for timers, promises, async/await behavior, and multi-threaded execution via RPC.

📦 Installation
--------------

[](#-installation)

Install via [Packagist/Composer](https://packagist.org/packages/mjohann/zynq):

```
composer require mjohann/zynq
```

⚙️ Requirements
---------------

[](#️-requirements)

- PHP 8.0 or higher

🚀 Features
----------

[](#-features)

**Zynq** is a PHP library that brings JavaScript-like asynchronous features to PHP, enabling a smoother and more efficient programming experience. The currently supported features include:

### ⏱️ Timers

[](#️-timers)

- **`setTimeout(callback, delay)`**: Executes a function after a specified delay.
- **`setInterval(callback, interval)`**: Repeatedly executes a function at fixed intervals.
- **`clearTimeout(timerId)`**: Cancels a timer previously created with `setTimeout`.
- **`clearInterval(timerId)`**: Cancels a timer previously created with `setInterval`.

### 🔄 Promises &amp; Flow Control

[](#-promises--flow-control)

- **`Promise`**: Native-like implementation for handling asynchronous operations.
- **`then(onFulfilled)`**: Defines what to do when a promise is fulfilled.
- **`catch(onRejected)`**: Handles errors when a promise is rejected.
- **`finally(onFinally)`**: Executes code after a promise is settled, regardless of the outcome.
- **`await`**: Waits for a promise to resolve before continuing execution.
- **`async`**: Declares an asynchronous function that returns a promise.

### 🧠 Asynchronous Execution &amp; RPC

[](#-asynchronous-execution--rpc)

- **`RPC` (Remote Procedure Call)**: Allows executing functions in separate processes using a lightweight RPC mechanism.
- **Multi-process Execution**: Simulates multithreading using child processes to improve performance in asynchronous and intensive tasks.

🧪 Usage Examples
----------------

[](#-usage-examples)

> 📺 [Full usage guide (previous version) on YouTube](https://www.youtube.com/watch?v=ZFbOnbJQN3U)

### ⏲️ Timers

[](#️-timers-1)

Zynq provides `setTimeout`, `setInterval`, `clearTimeout`, and `clearInterval`, mirroring their JavaScript counterparts.

#### Key Concepts

[](#key-concepts)

- A **callback** is a function passed as a parameter to another function. In PHP, callbacks are typically of type `callable`.
- PHP's `Closure` class is used to represent anonymous and arrow functions.
- Named functions can also be used as callbacks.

#### Functions

[](#functions)

- `setInterval(callback, milliseconds)` — Repeatedly executes the given callback every X milliseconds. Returns a unique ID (UID) for managing the interval.
- `setTimeout(callback, milliseconds)` — Executes the callback once after X milliseconds. Returns a UID.
- `clearInterval(UID)` — Stops the scheduled `setInterval` by UID. Returns `true` if successful, `false` otherwise.
- `clearTimeout(UID)` — Stops the scheduled `setTimeout` by UID. Returns `true` if successful, `false` otherwise.

#### Example

[](#example)

```
