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

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

ajbdev/php-struct
=================

Structs for PHP

46131PHP

Since Jun 29Pushed 10y ago3 watchersCompare

[ Source](https://github.com/ajbdev/php-struct)[ Packagist](https://packagist.org/packages/ajbdev/php-struct)[ RSS](/packages/ajbdev-php-struct/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Structs for PHP
===============

[](#structs-for-php)

Structs for PHP7 inspired by golang

Usage
-----

[](#usage)

```
struct('User', [
    'name'      =>  'string',
    'age'       =>  'int',
    'active'    =>  'bool',
]);

$user = new User();

$user['name'] = 'Andy';
$user['age'] = 13;
$user['active'] = true;

$user['email'] = 'andybaird@gmail.com';

// Fatal error: Uncaught InvalidArgumentException: Struct does not contain property `email`

$user['age'] = '22';

// Fatal error: Uncaught TypeException: Argument 1 passed to User::set_age() must be of the type integer, string given
```

Turn off strict type checking and allow variables to be coerced into types by simply calling:

```
Struct\Struct::$strict = false;
$user['age'] = '22';
var_dump($user['age']);

// int(22)
```

Under the hood, structs are simply classes implementing ArrayAccess and Iterable generated at run time. They have generated getter and setters for all fields that allow them to do the type checking.

Filling a struct from an array:

```
$row = $db->fetchArray('select * from user where id=1');
$user->fromArray($row);
```

You can extend structs further by giving them their own methods.

```
struct('User', [
    'firstName'         =>  'string',
    'lastName'          =>  'string',
    'active'            =>  'bool',
    'age'               =>  'int'
],[
    'fullName'          =>  function() {
        return $this['firstName'] . ' ' . $this['lastName'];
    }
]);

$user['firstName'] = 'Andy';
$user['lastName'] = 'Baird';

echo $user->fullName();
// Andy Baird
```

Add magic methods simply:

```
struct('User', [
    'firstName'         =>  'string',
    'lastName'          =>  'string',
    'active'            =>  'bool',
    'age'               =>  'int'
],[
    '__toString'          =>  function() {
        return $this['firstName'] . ' ' . $this['lastName'] . ' is a ' . $this['age'] . ' year old ' . ($this['active'] ? 'active' : 'inactive') . ' user';
    }
]);

echo $user;
// Andy Baird is a 13 year old inactive user
```

But... why?
-----------

[](#but-why)

Just for my own experimentation. I would love to see structs implemented as a core feature of PHP, as I can see them being very appropriate for a more procedural or functional style of programming.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/7b0842f020a6b6eb2158be81dd79dae672267fada8c073ee5861febfcbfbfbbe?d=identicon)[ajbdev](/maintainers/ajbdev)

---

Top Contributors

[![ajbdev](https://avatars.githubusercontent.com/u/572055?v=4)](https://github.com/ajbdev "ajbdev (18 commits)")

### Embed Badge

![Health badge](/badges/ajbdev-php-struct/health.svg)

```
[![Health](https://phpackages.com/badges/ajbdev-php-struct/health.svg)](https://phpackages.com/packages/ajbdev-php-struct)
```

###  Alternatives

[dereuromark/cakephp-dto

A CakePHP plugin for generating immutable Data Transfer Objects with full type safety

3096.0k5](/packages/dereuromark-cakephp-dto)[georgringer/faker

Faker for TYPO3

165.2k](/packages/georgringer-faker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
