PHPackages                             mindplay/session - 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. mindplay/session

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

mindplay/session
================

Type-hinted session container for PHP

2.0.0(10y ago)21.4k1[1 issues](https://github.com/mindplay-dk/session/issues)LGPL-3.0+PHPPHP &gt;=5.3.0

Since Sep 4Pushed 9y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (1)Versions (4)Used By (0)

mindplay/session
================

[](#mindplaysession)

This library implements a type-hinted container for session model objects.

This approach gives you type-hinted closures whenever you're working with session state of any sort, which is great for IDE support and code comprehension in general.

[![Build Status](https://camo.githubusercontent.com/03350b645b04003a09d5511a22f1b3f917f26d9785b7ce6f44a16a51d9239d21/68747470733a2f2f7472617669732d63692e6f72672f6d696e64706c61792d646b2f73657373696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mindplay-dk/session)

[![Code Coverage](https://camo.githubusercontent.com/3e87374ef133d709643cf71f9a3aeaede2375698d946e4991e6fe4ea2bfb3fb5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f73657373696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/session/?branch=master)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/04d28e817343776f5f60635e628e09a47c718a73f9b6997f894f022ef7e4c156/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f73657373696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/session/?branch=master)

### Usage

[](#usage)

Note that `SessionService` does not attempt to control the PHP session lifecycle - it only provides a type-safe means of storing serialized objects in session variables; you are still in charge of e.g. starting the session with `session_start()`, etc.

#### Setting up

[](#setting-up)

Let's assume you have a session model like this one:

```
class Cart
{
    /** @var int */
    public $user_id;

    /** @var int[] */
    public $product_ids = array();
}
```

In a real project, you probably want to use a dependency injection container or some other means of centrally managing your `SessionContainer` instance.

At the end of your request cycle (centrally, e.g. after dispatching a controller, but before sending the response), you must call `commit()` to store the session data:

```
// commit session container contents to session variables:

$session->commit();
```

This ensures you don't have partial changes made to session variables in case of errors. If you don't care about transactional sessions and want changes committed automatically, you can register a shutdown function, for example:

```
register_shutdown_function(function () use ($session) {
    $session->commit();
});
```

#### Working with the session model container

[](#working-with-the-session-model-container)

In the following examples, for simplicty, we'll assume your session container is a global variable:

```
use mindplay\session\SessionContainer;

$session = new SessionContainer();
```

Note that, in a real project, you would probably want to type-hint e.g. controllers against the abstract `SessionContainer` interface, since calling e.g. `commit()` isn't relevant there.

To access/update a session model object, pass a type-hinted closure to the `update()` method:

```
// add some products to the Cart:

$session->update(
    function (Cart $cart) {
        $cart->product_ids[] = 777;
        $cart->product_ids[] = 555;
    }
);
```

The `update()` method will construct `Cart` for you - it's therefore important to note that session model classes must always have an empty constructor.

You can take values out of a container as well:

```
$cart = $session->update(function (Cart $cart) {
    return $cart;
});
```

But do note that it's generally not very good practice to take session model objects out of the container, as this blurs the fact that you're making changes to session state - the call to `update()` clarifies what you're doing.

To remove a session model object:

```
// empty the cart:

$session->remove(Cart::class);
```

If you have a reference to the session model object, `remove()` will also accept that.

##### Optional session models

[](#optional-session-models)

If you don't know if a session model has been constructed yet, and you want to avoid creating an empty session model, you can use a default `null` parameter in the closure:

```
$session->update(
    function (Cart $cart = null) {
        if ($cart) {
            // ...
        }
    }
);
```

##### Multiple models in one call

[](#multiple-models-in-one-call)

If you need two (or more) session model objects at the same time, just ask for them:

```
$session->update(function (User $user, Cart $cart) {
    // ...
});
```

##### Remove all session models

[](#remove-all-session-models)

To remove all session models, call the `clear()` method.

Note that the session models are not removed from underlying storage until `commit()`is called.

### Custom storage and testing

[](#custom-storage-and-testing)

A `MockSessionStorage` implementation is included, useful for integration tests - you can provide this (or any `SessionStorage` implementation) at construction time, e.g.:

```
use mindplay\session\MockSessionStorage;
use mindplay\session\SessionContainer;

$storage = new MockSessionStorage('foo');

$container = new SessionContainer($storage);
```

During integration tests, you can make assertions about the contents of the public `MockSessionStorage::$data` property, which contains the raw session model objects, indexed by class-name.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community8

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 ~174 days

Total

3

Last Release

3925d ago

Major Versions

1.0.1 → 2.0.02015-08-18

### Community

Maintainers

![](https://www.gravatar.com/avatar/9445f567f43ee7a963270651e40e533634586f959e4df3d5398d001b1cb49be8?d=identicon)[mindplay.dk](/maintainers/mindplay.dk)

---

Top Contributors

[![mindplay-dk](https://avatars.githubusercontent.com/u/103348?v=4)](https://github.com/mindplay-dk "mindplay-dk (10 commits)")

### Embed Badge

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

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

###  Alternatives

[koenhendriks/laravel-str-acronym

A package to be able to generate acronyms from strings in Laravel projects using the Str helper and supports the Stringable class.

4352.9k](/packages/koenhendriks-laravel-str-acronym)

PHPackages © 2026

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