PHPackages                             swoole/typephp - 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. swoole/typephp

ActiveLibrary

swoole/typephp
==============

TypePHP native AOT compiler

v0.6.2(today)1569↑2900%5PHPPHP &gt;=8.4 &lt;8.6CI failing

Since Jan 31Pushed today3 watchersCompare

[ Source](https://github.com/swoole/typephp)[ Packagist](https://packagist.org/packages/swoole/typephp)[ RSS](/packages/swoole-typephp/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (10)Versions (14)Used By (0)

[English](README.md) | [简体中文](README-CN.md)

TypePHP
=======

[](#typephp)

**A native AOT compiler for PHP**

Compile PHP source code into native machine code ahead of time — producing native executables, PHP extensions, and shared libraries — while keeping the PHP syntax you already know.

[![Tests](https://github.com/swoole/typephp/actions/workflows/tests.yml/badge.svg)](https://github.com/swoole/typephp/actions/workflows/tests.yml)[![PHP 8.4–8.5](https://camo.githubusercontent.com/331558035dfa4385379c39cce8a15a81749e47fe4b16d5d0d08b8c3c441da8f4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e342d2d382e352d3737376262342e737667)](https://www.php.net/)[![License: GPL-3.0](https://camo.githubusercontent.com/c8e817d0fab13b6b935489e0692f5301982fdcc96451d589d7444f2055cf9a7c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d47504c2d2d332e302d626c75652e737667)](LICENSE)

---

What is TypePHP?
----------------

[](#what-is-typephp)

TypePHP is an Ahead-Of-Time (AOT) compiler that translates PHP source code into C++ and then into native machine code. Unlike a bytecode cache or a VM, it does not interpret opcodes at runtime: it generates optimized native binaries that run directly on the CPU.

It keeps familiar PHP syntax and adds compile-time type information, so the compiler can emit fast, statically-typed C++ for hot paths. Dynamic PHP values, internal functions, reflection, and object metadata continue to interoperate with the Zend runtime through PHPX; user functions are not executed as Zend opcodes after they have been compiled.

TypePHP is **written entirely in PHP** and is **fully self-hosting**: the `tpc`compiler binary is built by compiling the compiler's own PHP source code with TypePHP. The bootstrap chain is pure PHP — no C or C++ glue in the compiler itself.

TypePHP is under active development. It intentionally supports a defined, testable subset of PHP rather than claiming drop-in compatibility with every dynamic PHP program. Read [Compatibility model](#compatibility-model) and the [incompatible-feature list](docs/INCOMPATIBLE_PHP_FEATURES.md) before adopting it for an existing application.

How it works
------------

[](#how-it-works)

```
PHP source + .stub.php declarations + optional C/C++ sources
                         │
                         ▼
        parse, validate, and collect declarations
                         │
                         ▼
       lower function bodies and constants to C++17
                         │
                         ▼
       native compiler + reusable object/PCH caches
                         │
                         ▼
 executable | PHP extension | shared library | WASI component

```

The prepare phase builds the complete symbol model without allocating runtime cache IDs. Constants and declaration defaults retain their AST until the convert phase, where they are lowered after all project symbols are known. This two-phase design keeps multi-file and self-hosted builds deterministic.

Features
--------

[](#features)

- **Self-hosting, written in PHP** — the TypePHP compiler is implemented entirely in PHP and bootstraps itself: `tpc` compiles the compiler's own source into a native binary.
- **True AOT compilation** — PHP is lowered to C++17, then to native machine code. No interpreter, no opcode cache, no JIT warm-up.
- **Three native build modes** — build a native `bin` executable, a loadable PHP `ext` extension, or a reusable `lib` shared library from the same codebase.
- **Native type system** — `int`, `float`, and `bool` map directly to C++ scalar types (`int64_t`, `double`, `bool`) for orders-of-magnitude speedups on numeric code.
- **High-precision numerics** — `bigInt` (GMP), `decimal` (libmpdec), and `bigFloat` (MPFR), with typed operators and method APIs.
- **Strongly-typed containers** — `std::array`, `std::vector`, `std::map`, and `std::ordered_map` with compile-time element types; up to **10×** faster than PHP arrays and on par with C++ `std::vector`.
- **Universal methods** — call methods directly on primitives (`$s->upper()`, `$arr->contains()`, `$big->mul(2)`); statically-known calls are resolved directly at compile time.
- **Mixed C++ / PHP** — call C++ functions from PHP (and vice versa) for performance-critical kernels.
- **Compile-time functions &amp; keywords** — `any()`, `refval()`, `objval()`, `expected()`, `unexpected()`, plus `toInt()`, `toString()`, `toArray()` and friends.
- **Compile-time safety** — `#[Immutable]` read-only contracts and `#[ArrayDef]`array-shape metadata, checked at compile time with zero runtime cost.
- **Compile-time code generation** — `#[Getter]`, `#[Setter]`, `#[With]`, `#[Constructor]`, `#[Printer]`, and `#[Arrayable]` generate type-safe methods from property declarations.
- **Modern PHP support** — PHP 8.4 property hooks, asymmetric visibility, PHP 8.5 `clone()`-with, and `(void)` discard expressions.
- **Cross-platform &amp; WASM** — Linux, Windows, and macOS targets for x86-64 and ARM64, plus WASI 0.2 and browser (Jco) output.
- **Python bridge** — generate IDE helpers for Python modules and convert Python scripts to TypePHP.

Why TypePHP?
------------

[](#why-typephp)

TypePHP AOTOpcode cache (OPcache)JIT (PHP 8+)Compilation targetNative machine codeBytecodeMachine code (trace)Startup / warm-upNone (already compiled)Per-process warm-upJIT warm-upType-driven optimizationCompile-time, full-programNoneLimited, trace-basedNative executable outputYesNoNoSource code protectionCompiled to machine codeBytecode (reversible)Bytecode (reversible)Deterministic performanceYesNoNo**Strengths over plain PHP:**

- **Near-native performance.** Numeric and container-heavy hot paths compile down to the same machine code a C++ program would produce. See the [benchmark](#benchmark) below.
- **Source protection.** Your source is compiled away — shipped artifacts are native binaries, not readable PHP files.
- **Native process entry.** Binary mode starts directly from a native executable and does not require the PHP CLI or a separate interpreter process. The executable still embeds/links PHPX, `libphp`, and any configured native libraries, which must be available in the deployment package.
- **Gradual typing that actually pays off.** Add `use native_types`, `std::`containers, and type declarations only where performance matters; the rest stays ordinary PHP.
- **Zend ecosystem interop.** Extension mode loads as a standard PHP extension, and projects can call supported internal functions and require other Zend extensions explicitly.

Requirements
------------

[](#requirements)

- **PHP 8.4 – 8.5** CLI, development headers, and `php-config`
- The matching **PHP embed library** (`libphp.so`) for binary/shared-library builds on Unix-like systems
- **GCC 9+** (or Clang) with **C++17**
- **CMake 3.24+**
- **Composer 2**
- High-precision math libraries: **GMP**, **MPFR** (libmpdec is bundled with PHPX)

```
# Ubuntu/Debian
sudo apt install build-essential cmake pkg-config libgmp-dev libmpfr-dev

# RHEL/CentOS/Fedora
sudo dnf install gcc gcc-c++ cmake pkgconf-pkg-config gmp-devel mpfr-devel

# Arch Linux
sudo pacman -S base-devel cmake pkgconf gmp mpfr
```

> GMP powers `bigInt` and MPFR powers `bigFloat`. The `decimal` type is backed by libmpdec, which is bundled with PHPX — no separate install required.

Linux is the primary development and CI platform. The compiler also has Windows, macOS, x86-64, ARM64, and WASI backends; availability of PHP embed, toolchain, and third-party libraries still determines which target can be built on a given host.

Installation
------------

[](#installation)

### Via Composer

[](#via-composer)

```
composer require --dev swoole/typephp
```

Then compile your project:

```
vendor/bin/tpc.php project.yml
```

When working inside the TypePHP source repository, use the local entry point instead:

```
bin/tpc.php project.yml
```

### From source

[](#from-source)

```
git clone https://github.com/swoole/typephp.git
cd typephp
composer install
php bin/tpc.php --help
```

`PHPX_HOME` may point to a separate PHPX checkout or installation. `PHP_HOME`may point to the PHP embed prefix; it must contain `bin/php-config`, PHP headers, and `lib/libphp.so` on Unix-like systems.

### Building `libphp.so`

[](#building-libphpso)

Binary and shared-library builds require PHP's `embed` SAPI. If `libphp.so` is missing on Linux, `tpc.php` can interactively download the PHP source and build it for you. A PHP extension build resolves Zend symbols from the host SAPI and must not load a second `libphp`. See [Automatic libphp.so build](docs/LIBPHP_INSTALLER.md).

Quick Start
-----------

[](#quick-start)

Create `hello.php`:

```
