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

ActiveLibrary

coroq/session
=============

Session-backed typed objects for PHP 8.0+

v1.0.1(3mo ago)1164[1 issues](https://github.com/ozami/coroq-session/issues)[1 PRs](https://github.com/ozami/coroq-session/pulls)MITPHPPHP ^8.0CI failing

Since Aug 31Pushed 2mo ago1 watchersCompare

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

READMEChangelog (9)Dependencies (1)Versions (18)Used By (0)

coroq/session
=============

[](#coroqsession)

Session-backed typed objects for PHP 8.0+.

Requirements
------------

[](#requirements)

- PHP 8.0+

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

[](#installation)

```
composer require coroq/session
```

What it does
------------

[](#what-it-does)

Manage session data as typed objects. You get IDE completion, type checking, and refactoring support for session data.

Quick Start
-----------

[](#quick-start)

```
use Coroq\Session\Session;

class UserSession extends Session {
    public string $name = '';
    public bool $isLoggedIn = false;
}

$user = new UserSession('user');  // Loads values from $_SESSION['user']
$user->name = 'John';
$user->isLoggedIn = true;
// Saved to $_SESSION['user'] = ['name' => 'John', 'isLoggedIn' => true]
```

Concept
-------

[](#concept)

Your class properties are stored in `$_SESSION` under the key you pass to the constructor.

```
$user = new UserSession('user');
// Internally: $_SESSION['user'] = ['name' => 'John', 'isLoggedIn' => true]

$cart = new CartSession('cart');
// Internally: $_SESSION['cart'] = ['items' => [...]]
```

The same class can be used with different keys:

```
$currentUser = new UserSession('current_user');
$editingUser = new UserSession('editing_user');
```

Examples
--------

[](#examples)

### Shopping Cart

[](#shopping-cart)

```
class CartItem {
    public function __construct(
        public string $productId,
        public int $quantity = 1,
        public int $price = 0,
    ) {}
}

class CartSession extends Session {
    /** @var CartItem[] */
    public array $items = [];
    public int $totalPrice = 0;
}

$cart = new CartSession('cart');
$cart->items[] = new CartItem('prod_1', 2, 1000);
$cart->items[] = new CartItem('prod_2', 1, 500);
$cart->totalPrice = 2500;

// Stored as:
// $_SESSION['cart'] = [
//     'items' => [CartItem(...), CartItem(...)],
//     'totalPrice' => 2500,
// ]
```

### Multiple Session Keys

[](#multiple-session-keys)

```
class FormSession extends Session {
    public array $data = [];
    public array $errors = [];
}

$contactForm = new FormSession('form_contact');
$loginForm = new FormSession('form_login');
```

What kind of data can be stored in session
------------------------------------------

[](#what-kind-of-data-can-be-stored-in-session)

PHP serializes (encodes as string) session data when storing it. This is general PHP behavior, not specific to this library.

**Safe to store:**

- Scalars (string, int, bool, float)
- Arrays
- Simple objects that only hold data ("Data Transfer Object")

**Avoid storing:**

- Objects with resources (file handles, database connections)
- Objects with dependencies (services, repositories)
- Objects with complex internal state

Handling type mismatch
----------------------

[](#handling-type-mismatch)

When property types change between deployments, stored session data may no longer match the expected types. In this case, properties keep their default values. Override `onTypeError()` to log or handle:

```
class UserSession extends Session {
    public int $version = 2;
    public string $name = '';

    protected function onTypeError(array $storedValues, \TypeError $error): void {
        error_log('Session type mismatch: ' . $error->getMessage());
    }
}
```

Migration from v1.x
-------------------

[](#migration-from-v1x)

v2.0 is a complete redesign using typed properties. The previous implementation is available as `ArraySession` for backward compatibility.

```
// Before (v1.x)
use Coroq\Session\Session;

$session = new Session('user');
$session->set(['name' => 'John']);
$name = $session->getIn('name');

// After (v2.x) - change import to ArraySession
use Coroq\Session\ArraySession;

$session = new ArraySession('user');
$session->set(['name' => 'John']);
$name = $session->getIn('name');
```

ArraySession is deprecated. For new code, use `Session` instead.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance63

Regular maintenance activity

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity75

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

Recently: every ~139 days

Total

14

Last Release

65d ago

Major Versions

0.5.4 → v1.0.02025-11-23

PHP version history (3 changes)0.1.0PHP &gt;=5.4

0.5.0PHP &gt;=7.2

v1.0.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![ozami](https://avatars.githubusercontent.com/u/170309?v=4)](https://github.com/ozami "ozami (24 commits)")

---

Tags

sessiontype-safe

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[nette/http

🌐 Nette Http: abstraction for HTTP request, response and session. Provides careful data sanitization and utility for URL and cookies manipulation.

48619.2M541](/packages/nette-http)[yiisoft/yii2-redis

Redis Cache, Session and ActiveRecord for the Yii framework

48011.7M245](/packages/yiisoft-yii2-redis)[laminas/laminas-session

Object-oriented interface to PHP sessions and storage

8122.7M113](/packages/laminas-laminas-session)[mezzio/mezzio-session

Session container and middleware for PSR-7 applications

24982.3k16](/packages/mezzio-mezzio-session)[epic-64/elem

A fluent, type-safe PHP library for building HTML documents using the DOM

272.9k](/packages/epic-64-elem)

PHPackages © 2026

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