PHPackages                             php-unified/state - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. php-unified/state

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

php-unified/state
=================

A PHP standard for application states.

1.0.4(6y ago)23MITPHPPHP ^7.2CI failing

Since Jan 10Pushed 6y ago1 watchersCompare

[ Source](https://github.com/php-unified/state)[ Packagist](https://packagist.org/packages/php-unified/state)[ RSS](/packages/php-unified-state/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (0)

[![Build Status](https://camo.githubusercontent.com/549f22fe70bed5ed1785313976155ba5d074cd9b0d7ad0b36f5e54ca448f29f7/68747470733a2f2f7472617669732d63692e636f6d2f7068702d756e69666965642f73746174652e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/php-unified/state)

PHP Unified State
=================

[](#php-unified-state)

This package provides a standard for tracking states of applications in PHP. The package aims to create a multi-use standard.

This package contains interfaces and a simple implementations for tracking states.

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

[](#installation)

```
composer require php-unified/state
```

Usage
-----

[](#usage)

### Provided interfaces

[](#provided-interfaces)

#### PhpUnified\\State\\Common\\StateTrackerInterface

[](#phpunifiedstatecommonstatetrackerinterface)

This interface is an indication that an optional `StateInterface` interface can be supplied. The class that would implement this interface is one that keeps track of value for a state (optionally).

#### PhpUnified\\State\\Common\\StateInterface

[](#phpunifiedstatecommonstateinterface)

This interface provides a standard for implementing state tracking into applications. The following functions are expected to be implemented.

For this interface, an implementation is provided. See: `PhpUnified\State\State`.

##### Setters and getters

[](#setters-and-getters)

The object has 2 setters and 2 getters. These methods are:

- `setValue` and `getValue` for setting and getting the value of the object.
- `setIdentifier` and `getIdentifier` for setting and getting the identifier of the object.

##### Exporting states

[](#exporting-states)

###### String conversion

[](#string-conversion)

The output of the method `__toString` should convert a state to a human readable string. The result of this method with a single state could look like:

```
version: 1.0.0

```

Where `version` is the set identifier and `1.0.0` the set version.

For a state with multiple sub-states, output could look like:

```
packages:
    name: php-unified/state
    version: 1.0.0

```

Every line is a new state, with their respective `identifier: value` pairs.

###### Array conversion

[](#array-conversion)

The method for exporting states to an array is `exportState`. The output of the method could look like:

```
$output = [
    'packages' => '',
    'states' => [
        'name' => 'php-unified/state',
        'version' => '1.0.0'
    ]
]
```

##### Adding a sub-state

[](#adding-a-sub-state)

To add a state to a state, the method `addState` is expected to be implemented. These states should be iterated over in both of the exporting states implementations.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Tips
----

[](#tips)

### Using nested states

[](#using-nested-states)

By taking the approach of using nested states, the application can actively update their values. This would result a single repository of states, which can be used in logging. And thus creating more insight for debugging purposes.

For example, the application could monitor the last action from a database connection like so:

```
use PhpUnified\State\State;
use Application\Database\Connection;
use Application\Logger\Logger;

$systemState = new State();
$systemState->setIdentifier('system-state');

$databaseTransactionState = new State();
$databaseTransactionState->setIdentifier('last-db-transaction-state');

$systemState->addState($databaseTransactionState);

$logger = new Logger();
$connection = new Connection($databaseTransactionState);
try {
    $connection->doQuery();
} catch (Throwable $e) {
    $logger->log('warning', $e->getMessage());
    $logger->info($systemState->__toString());
}
```

Then the (fictional) Connection class could look like:

```
namespace Application\Database;

use PhpUnified\State\Common\StateInterface;

class Connection
{
    /**
     * State tracker
     *
     * @var StateInterface
     */
    private $state;

    /**
     * Constructor
     *
     * @param StateInterface $state
     */
    public function __construct(StateInterface $state)
    {
        $this->state = $state;
    }

    /**
     * Fictional doQuery function
     *
     * @return void
     */
    public function doQuery(): void
    {
        $state->setValue('Starting database connection');
        $connection = connectToDatabase();

        $state->setValue('Starting transaction');
        $connection->startTransaction();

        $state->setValue('Executing transaction');
        if($connection->doTransaction()) {
            $state->setValue(
                sprintf(
                    'Transaction with id %d transaction success',
                    $connection->getLastTransactionId()
                )
            );
            return true;
        }

        $state->setValue('Rolling back transaction');
        $connection->rollBackTransaction();
        $state->setValue('Rollback executed');
    }
}
```

If any of the above function calls in the `doQuery` function would result in an exception being thrown, the latest state can still be logged.

MIT License
-----------

[](#mit-license)

Copyright (c) 2019 GrizzIT

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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.

###  Release Activity

Cadence

Every ~105 days

Total

5

Last Release

2262d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8682881?v=4)[GrizzIT](/maintainers/GrizzIT)[@grizzit](https://github.com/grizzit)

---

Top Contributors

[![mfrankruijter](https://avatars.githubusercontent.com/u/8653925?v=4)](https://github.com/mfrankruijter "mfrankruijter (8 commits)")

---

Tags

guidelinephpstandardstatestate

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/php-unified-state/health.svg)

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

###  Alternatives

[phpdocumentor/type-resolver

A PSR-5 based resolver of Class names, Types and Structural Element Names

9.2k719.5M166](/packages/phpdocumentor-type-resolver)[pimple/pimple

Pimple, a simple Dependency Injection Container

2.7k130.5M1.4k](/packages/pimple-pimple)[league/container

A fast and intuitive dependency injection container.

86387.8M343](/packages/league-container)[stella-maris/clock

A pre-release of the proposed PSR-20 Clock-Interface

7947.5M2](/packages/stella-maris-clock)[wptrt/wpthemereview

PHP\_CodeSniffer rules (sniffs) to verify theme compliance with the rules for theme hosting on wordpress.org

217736.5k29](/packages/wptrt-wpthemereview)[inpsyde/modularity

Modular PSR-11 implementation for WordPress plugins, themes or libraries.

54383.3k3](/packages/inpsyde-modularity)

PHPackages © 2026

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