PHPackages                             spruct/spruct - 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. spruct/spruct

ActiveLibrary

spruct/spruct
=============

Clean PHP struct implementation with optional strong typed fields

1.0.2(11y ago)674MITPHPPHP &gt;=5.4

Since Jun 2Pushed 11y ago1 watchersCompare

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

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Spruct
======

[](#spruct)

[![Build Status](https://camo.githubusercontent.com/c1fd0dc4c073292bae802502b9310aa8e81fa4dafd82b6580d93fc507a343d2a/68747470733a2f2f7472617669732d63692e6f72672f6d617263696f416c6d6164612f7370727563742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/marcioAlmada/spruct)[![Coverage Status](https://camo.githubusercontent.com/01b629f492835de3b3ccde284cd1147340746e59f73b79bd366daf076541f462/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d617263696f416c6d6164612f7370727563742f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/marcioAlmada/spruct?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7d54f1e30e420b81c9b9a5e5bc6398917144edc87a6519cb931144519b65a8d5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d617263696f416c6d6164612f7370727563742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/marcioAlmada/spruct/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/7c0994d1cd1d6e0646bb38db49e484e7a7e096e6788a4930277a1cbfc166f53d/68747470733a2f2f706f7365722e707567782e6f72672f7370727563742f7370727563742f762f737461626c652e737667)](https://packagist.org/packages/spruct/spruct)[![Total Downloads](https://camo.githubusercontent.com/dd1a90ce26266813e9c9494da23dc643ebc5b963f8377d6f6ecbca54ff5bdfa2/68747470733a2f2f706f7365722e707567782e6f72672f7370727563742f7370727563742f646f776e6c6f6164732e737667)](https://packagist.org/packages/spruct/spruct)[![License](https://camo.githubusercontent.com/9bd7168e2b475d11ff90d942a5ecee6073ec28523fabe0013330d752e9fd6225/68747470733a2f2f706f7365722e707567782e6f72672f7370727563742f7370727563742f6c6963656e73652e737667)](https://packagist.org/packages/spruct/spruct)[![SensioLabsInsight](https://camo.githubusercontent.com/617b6a7f7ec81352ed50dc4c9d9c0467b2dd1b7062d48891c0e9f17823d68fea/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f31333163333264622d333834332d346561322d386362352d6365623532646532323330312f6d696e692e706e67)](https://insight.sensiolabs.com/projects/131c32db-3843-4ea2-8cb5-ceb52de22301)

Spruct gives you a clean PHP struct implementation with optional strong typed fields.

Composer Installation
---------------------

[](#composer-installation)

```
{
  "require": {
    "spruct/spruct": "~1.0"
  }
}
```

Through terminal: `composer require spruct/spruct:~1.0` 🎱

Usage
-----

[](#usage)

### Declaring Structs

[](#declaring-structs)

A new struct type can be created by extending the abstract `\Spruct\Struct` class:

```
/**
 * Struct representing a 2D point
 */
class D2Point extends \Spruct\Struct
{
    /** @struct.type boolean */
    protected $visible = false;

    /** @struct.type float */
    protected $x;

    /** @struct.type float */
    protected $y;
}
```

Fields are declared through protected properties and data types are specified through the `@struct.type` property annotation:

```
/**
 * @struct.type
 */
  protected $field;
```

### Valid Type Declarations:

[](#valid-type-declarations)

TypeTokensExampleboolean`bool`, `boolean``/** @struct.type boolean */`integer`integer`, `int``/** @struct.type integer */`string`string`, `str``/** @struct.type string */`double`double`, `float``/** @struct.type float */`array`array``/** @struct.type array */`Classfull qualified class name`/** @struct.type \Some\Existing\Class */`regexa valid regex expression`/** @struct.type #^\w{3}\d+$# */`### Initializing Structs

[](#initializing-structs)

Structs can be initialized with a key value array prototype, like this:

```
$point = new D2Point([
    'x' => 1.0,
    'y' => 2.0
]);
```

### Manipulating Structs

[](#manipulating-structs)

Structs can be manipulated just like a common `\stdClass` instance:

```
$pointA = new D2Point();
$pointA->visible = false;
$pointA->x = 1.0;
$pointA->y = 1.5;
```

Struct exception messages are pretty self explanatory `\Spruct\StructException`:

```
$pointB = new D2Point();
$pointB->visible = 'y'; // ! Cannot use string(y) as type float in field D2Point->visible
$pointB->x = 1;         // ! Cannot use integer(1) as type float in field D2Point->x
$pointB->y = [];        // ! Cannot use array as type float in field D2Point->y
```

### Required Fields

[](#required-fields)

You can also declare fields that must not be null using `@struct.requires` class annotation:

```
/**
 * @struct.requires name, age, role
 */
class Employee extends \Spruct\Struct
{
    /** @struct.type string */
    protected $name;

    /** @struct.type string */
    protected $role;

    /** @struct.type integer */
    protected $age;

    /** @struct.type #^\d{8}$# */
    protected $code;
}
```

Required fields are validated during struct initialization. If required fields are missing, a `\Spruct\StructException` is thrown.

```
new Employee(['age' => 21]);  // ! Cannot initialize Employee with null ["name", "role"]
```

Contributing
------------

[](#contributing)

1. Fork [spruct\\spruct](https://github.com/marcioAlmada/spruct/fork) and clone
2. Install composer dependencies `$ composer install`
3. Run unit tests with phpunit 4.2+
4. Modify code: correct bug, implement feature
5. Back to step 3

> NOTICE: phpunit 4.2+ is required due to a feature introduced [here](https://github.com/sebastianbergmann/phpunit/issues/1263)

Copyright
---------

[](#copyright)

Copyright (c) 2014 Márcio Almada. Distributed under the terms of an MIT-style license. See LICENSE for details.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~53 days

Total

3

Last Release

4261d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e6b4f6d6d37192719fb4d4d03bbdb9e866972a2b6556da9967b10b35843809f1?d=identicon)[marcioAlmada](/maintainers/marcioAlmada)

---

Top Contributors

[![marcioAlmada](https://avatars.githubusercontent.com/u/227395?v=4)](https://github.com/marcioAlmada "marcioAlmada (46 commits)")

---

Tags

typedatastandardstruct

### Embed Badge

![Health badge](/badges/spruct-spruct/health.svg)

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

###  Alternatives

[fakerphp/faker

Faker is a PHP library that generates fake data for you.

3.9k358.5M3.5k](/packages/fakerphp-faker)[phpoption/phpoption

Option Type for PHP

2.7k541.2M159](/packages/phpoption-phpoption)[symfony/property-info

Extracts information about PHP class' properties using metadata of popular sources

2.2k256.7M854](/packages/symfony-property-info)[nelmio/alice

Expressive fixtures generator

2.5k43.4M133](/packages/nelmio-alice)[dflydev/dot-access-data

Given a deep data structure, access data by dot notation.

718359.1M86](/packages/dflydev-dot-access-data)[dealerdirect/phpcodesniffer-composer-installer

PHP\_CodeSniffer Standards Composer Installer Plugin

596161.9M1.9k](/packages/dealerdirect-phpcodesniffer-composer-installer)

PHPackages © 2026

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