# php/php-src architecture

> Architecture diagram of the GitHub repository php/php-src, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/php/php-src
- Repository: https://github.com/php/php-src

## Overview

PHP is a general-purpose scripting-language interpreter with bundled extensions. Its principal workflow is a script entering through a SAPI, being compiled into engine instructions, and executed to produce program behavior. The graph also shows the optimizer, object handling, shared runtime facilities, and representative extensions. Relationships are conservative: unsampled runtime call paths are dashed and unverified; solid edges are supported by the README or SOURCE INDEX references.

Main entry points: [`main/main.c`](https://github.com/php/php-src/blob/master/main/main.c) (PHP runtime), [`Zend/zend_API.h`](https://github.com/php/php-src/blob/master/Zend/zend_API.h) (Engine API), [`Zend/zend_object_handlers.c`](https://github.com/php/php-src/blob/master/Zend/zend_object_handlers.c) (Object handlers), [`main/SAPI.h`](https://github.com/php/php-src/blob/master/main/SAPI.h) (SAPI interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Runtime entry"]
  node_sapi["SAPI interface<br/>[SAPI.h]"]
  node_runtime_main["PHP runtime<br/>[main.c]"]
end

subgraph group_engine["Zend engine"]
  node_compiler["Compiler<br/>[zend_compile.c]"]
  node_optimizer["Optimizer<br/>[zend_optimizer.c]"]
  node_executor["Instruction executor<br/>[zend_execute.c]"]
  node_vm["VM handlers<br/>[zend_vm_execute.h]"]
  node_objects["Object handlers"]
  node_engine_api["Engine API<br/>[zend_API.h]"]
end

subgraph group_runtime["Runtime services"]
  node_engine_types["Engine types<br/>[zend_types.h]"]
  node_memory["Memory allocation<br/>[zend_alloc.h]"]
  node_hashes["Hash tables<br/>[zend_hash.h]"]
  node_variables["Variables<br/>[zend_variables.h]"]
  node_operators["Operators<br/>[zend_operators.h]"]
  node_exceptions["Exceptions<br/>[zend_exceptions.h]"]
  node_streams["PHP streams<br/>[php_streams.h]"]
end

subgraph group_extensions["Bundled extensions"]
  node_random["Random APIs<br/>[php_random.h]"]
  node_random_algorithms["Random algorithms<br/>[engine_mt19937.c]"]
  node_date["Date and time<br/>[php_date.c]"]
  node_dom["DOM and XML<br/>[php_dom.c]"]
  node_curl["cURL extension<br/>[interface.c]"]
end

node_script["PHP script"]

node_script -.->|"enters through"| node_sapi
node_sapi -.->|"dispatches to"| node_runtime_main
node_runtime_main -.->|"invokes"| node_compiler
node_compiler -.->|"optimizes"| node_optimizer
node_runtime_main -.->|"invokes"| node_executor
node_executor -.->|"dispatches to"| node_vm
node_runtime_main -->|"includes API"| node_engine_api
node_runtime_main -->|"includes streams"| node_streams
node_engine_api -->|"references"| node_variables
node_engine_api -->|"references"| node_operators
node_objects -->|"uses API"| node_engine_api
node_objects -->|"uses exceptions"| node_exceptions
node_runtime_main -.->|"uses allocation"| node_memory
node_runtime_main -.->|"uses tables"| node_hashes
node_random -.->|"exposes algorithms"| node_random_algorithms

click node_sapi "https://github.com/php/php-src/blob/master/main/SAPI.h"
click node_runtime_main "https://github.com/php/php-src/blob/master/main/main.c"
click node_compiler "https://github.com/php/php-src/blob/master/Zend/zend_compile.c"
click node_optimizer "https://github.com/php/php-src/blob/master/Zend/Optimizer/zend_optimizer.c"
click node_executor "https://github.com/php/php-src/blob/master/Zend/zend_execute.c"
click node_vm "https://github.com/php/php-src/blob/master/Zend/zend_vm_execute.h"
click node_objects "https://github.com/php/php-src/blob/master/Zend/zend_object_handlers.c"
click node_engine_api "https://github.com/php/php-src/blob/master/Zend/zend_API.h"
click node_engine_types "https://github.com/php/php-src/blob/master/Zend/zend_types.h"
click node_memory "https://github.com/php/php-src/blob/master/Zend/zend_alloc.h"
click node_hashes "https://github.com/php/php-src/blob/master/Zend/zend_hash.h"
click node_variables "https://github.com/php/php-src/blob/master/Zend/zend_variables.h"
click node_operators "https://github.com/php/php-src/blob/master/Zend/zend_operators.h"
click node_exceptions "https://github.com/php/php-src/blob/master/Zend/zend_exceptions.h"
click node_streams "https://github.com/php/php-src/blob/master/main/php_streams.h"
click node_random "https://github.com/php/php-src/blob/master/ext/random/php_random.h"
click node_random_algorithms "https://github.com/php/php-src/blob/master/ext/random/engine_mt19937.c"
click node_date "https://github.com/php/php-src/blob/master/ext/date/php_date.c"
click node_dom "https://github.com/php/php-src/blob/master/ext/dom/php_dom.c"
click node_curl "https://github.com/php/php-src/blob/master/ext/curl/interface.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sapi,node_runtime_main toneBlue
class node_compiler,node_optimizer,node_executor,node_vm,node_objects,node_engine_api toneAmber
class node_engine_types,node_memory,node_hashes,node_variables,node_operators,node_exceptions,node_streams toneMint
class node_random,node_random_algorithms,node_date,node_dom,node_curl toneRose
class node_script toneIndigo
```

## Components

### Runtime entry

- [SAPI interface](https://github.com/php/php-src/blob/master/main/SAPI.h): `main/SAPI.h`
- [PHP runtime](https://github.com/php/php-src/blob/master/main/main.c): `main/main.c`

### Zend engine

- [Compiler](https://github.com/php/php-src/blob/master/Zend/zend_compile.c): `Zend/zend_compile.c`
- [Optimizer](https://github.com/php/php-src/blob/master/Zend/Optimizer/zend_optimizer.c): `Zend/Optimizer/zend_optimizer.c`
- [Instruction executor](https://github.com/php/php-src/blob/master/Zend/zend_execute.c): `Zend/zend_execute.c`
- [VM handlers](https://github.com/php/php-src/blob/master/Zend/zend_vm_execute.h): `Zend/zend_vm_execute.h`
- [Object handlers](https://github.com/php/php-src/blob/master/Zend/zend_object_handlers.c): `Zend/zend_object_handlers.c`
- [Engine API](https://github.com/php/php-src/blob/master/Zend/zend_API.h): `Zend/zend_API.h`

### Runtime services

- [Engine types](https://github.com/php/php-src/blob/master/Zend/zend_types.h): `Zend/zend_types.h`
- [Memory allocation](https://github.com/php/php-src/blob/master/Zend/zend_alloc.h): `Zend/zend_alloc.h`
- [Hash tables](https://github.com/php/php-src/blob/master/Zend/zend_hash.h): `Zend/zend_hash.h`
- [Variables](https://github.com/php/php-src/blob/master/Zend/zend_variables.h): `Zend/zend_variables.h`
- [Operators](https://github.com/php/php-src/blob/master/Zend/zend_operators.h): `Zend/zend_operators.h`
- [Exceptions](https://github.com/php/php-src/blob/master/Zend/zend_exceptions.h): `Zend/zend_exceptions.h`
- [PHP streams](https://github.com/php/php-src/blob/master/main/php_streams.h): `main/php_streams.h`

### Bundled extensions

- [Random APIs](https://github.com/php/php-src/blob/master/ext/random/php_random.h): `ext/random/php_random.h`
- [Random algorithms](https://github.com/php/php-src/blob/master/ext/random/engine_mt19937.c): `ext/random/engine_mt19937.c`
- [Date and time](https://github.com/php/php-src/blob/master/ext/date/php_date.c): `ext/date/php_date.c`
- [DOM and XML](https://github.com/php/php-src/blob/master/ext/dom/php_dom.c): `ext/dom/php_dom.c`
- [cURL extension](https://github.com/php/php-src/blob/master/ext/curl/interface.c): `ext/curl/interface.c`

### Other components

- **PHP script**

## Connections

- PHP script → SAPI interface: enters through
- SAPI interface → PHP runtime: dispatches to (evidence: `main/main.c`)
- PHP runtime → Compiler: invokes
- Compiler → Optimizer: optimizes
- PHP runtime → Instruction executor: invokes
- Instruction executor → VM handlers: dispatches to
- PHP runtime → Engine API: includes API (evidence: `main/main.c`)
- PHP runtime → PHP streams: includes streams (evidence: `main/main.c`)
- Engine API → Variables: references (evidence: `Zend/zend_API.h`)
- Engine API → Operators: references (evidence: `Zend/zend_API.h`)
- Object handlers → Engine API: uses API (evidence: `Zend/zend_object_handlers.c`)
- Object handlers → Exceptions: uses exceptions (evidence: `Zend/zend_object_handlers.c`)
- PHP runtime → Memory allocation: uses allocation
- PHP runtime → Hash tables: uses tables
- Random APIs → Random algorithms: exposes algorithms (evidence: `ext/random/engine_mt19937.c`)
