承接 coroq/session 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

coroq/session

Composer 安装命令:

composer require coroq/session

包简介

Session-backed typed objects for PHP 8.0+

README 文档

README

Session-backed typed objects for PHP 8.0+.

Requirements

  • PHP 8.0+

Installation

composer require coroq/session

What it does

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

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

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

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

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

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

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

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.

统计信息

  • 总下载量: 165
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固