PHPackages                             rayblair06/php-struct - 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. rayblair06/php-struct

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

rayblair06/php-struct
=====================

A lightweight PHP class that lets you define C-style structs using PHP FFI, enabling memory-efficient, typed data structures for high-performance workloads.

0.1.0(6mo ago)31MITPHPCI passing

Since Nov 10Pushed 6mo agoCompare

[ Source](https://github.com/rayblair06/php-struct)[ Packagist](https://packagist.org/packages/rayblair06/php-struct)[ Docs](https://github.com/rayblair06/php-struct)[ RSS](/packages/rayblair06-php-struct/feed)WikiDiscussions main Synced 1mo ago

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

PHP Struct — Memory-Efficient Typed Arrays via FFI
==================================================

[](#php-struct--memory-efficient-typed-arrays-via-ffi)

A lightweight PHP class that lets you define **C-style structs** using [PHP FFI](https://www.php.net/manual/en/book.ffi.php), enabling **memory-efficient, typed data structures** for high-performance workloads.

Instead of juggling large PHP arrays with unpredictable memory usage, `Struct` lets you allocate a fixed-size, type-safe memory block for predictable performance and compact memory layout — just like in C.

### 💡 Inspiration

[](#-inspiration)

This project is inspired by the simplicity of C data structures, adapted for PHP developers who need **predictable memory layouts** and **FFI-level performance** without writing C extensions.

### 🧠 Example Use Cases

[](#-example-use-cases)

- Simulation / physics data storage
- Game development prototypes
- Data streaming and compression
- Machine learning preprocessing in PHP
- Memory-efficient caches or lookup tables

Summary
-------

[](#summary)

- [💡 Inspiration](#-inspiration)
- [🧠 Example Use Cases](#-example-use-cases)
- [🚀 Features](#-features)
- [🧰 Requirements](#-requirements)
- [🧪 Installation](#-installation)
- [🧱 Example Usage](#-example-usage)
- [🔍 API Reference](#-api-reference)
- [⚠️ Caveats](#-caveats)
    - [General](#general)
    - [Memory Management](#memory-management)
    - [Not Thread-Safe](#-not-thread-safe-in-parallel-ffi-contexts)
    - [Struct Layouts](#structs-must-use-fixed-layouts)
    - [Alignment and Padding](#alignment-and-padding)
    - [Strings and Character Arrays](#strings-and-character-arrays)
    - [Cross-Platform Differences](#cross-platform-differences)
    - [Type Validation and Safety](#type-validation-and-safety)
    - [Serialization &amp; Persistence](#serialization--persistence)
    - [Floating-Point Precision Notice](#floating-point-precision-notice)
- [⚙️ Performance Benchmark](#-performance-benchmark)
- [📄 License](#-license)

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

[](#-features)

- Define custom C structs at runtime using `typedef`
- Store thousands of records in fixed-size, contiguous memory
- Typed memory fields (`int`, `float`, etc.)
- Implements `Iterator` and `Countable`
- Provides `sizeof()` and total memory usage
- Ideal for numerical data, simulation, or high-frequency workloads

🧰 Requirements
--------------

[](#-requirements)

- PHP **8.0+**
- [FFI extension](https://www.php.net/manual/en/book.ffi.php) enabled (`--with-ffi`)
- CLI SAPI (FFI is not enabled by default in web contexts)

🧪 Installation
--------------

[](#-installation)

Install via Composer once packaged:

```
composer require rayblair06/php-struct
```

🧱 Example Usage
---------------

[](#-example-usage)

```
