PHPackages                             ap-lib/context - 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. ap-lib/context

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

ap-lib/context
==============

The Context library provides a lightweight context storage system for managing and retrieving shared data across different parts of an application. It supports dynamic storage, object retrieval, and type enforcement.

053PHP

Since Apr 2Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/ap-lib/context)[ Packagist](https://packagist.org/packages/ap-lib/context)[ RSS](/packages/ap-lib-context/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

AP\\Context
===========

[](#apcontext)

[![MIT License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

The Context library provides a lightweight context storage system for managing and retrieving shared data across different parts of an application. It supports dynamic storage, object retrieval, and type enforcement.

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

[](#installation)

```
composer require ap-lib/normalizer
```

Features
--------

[](#features)

- Store and retrieve arbitrary values using string keys.
- Store objects and retrieve them with type enforcement.
- Prevent accidental overwrites with an optional `replace` flag.
- Retrieve stored values by name or class.
- Exception handling for invalid operations.

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

[](#requirements)

- PHP 8.3 or higher

Getting started
---------------

[](#getting-started)

### Storing and Retrieving a Single Object

[](#storing-and-retrieving-a-single-object)

If you store an object without a name, its class name is automatically used as the key:

```
use AP\Context\Context;

class User {
    public function __construct(public int $id, public string $email) {}
}

$context = new Context();

$user = new User(12, "name@gmail.com");

// Store the object using its class name
$context->set($user);

// Retrieve the object by class name
$retrievedUser = $context->get(User::class);
var_dump($retrievedUser === $user); // true

// Retrieve with type enforcement
$retrievedUser = $context->getObject(User::class);
var_dump($retrievedUser === $user); // true
```

### Storing and Retrieving Multiple Similar Objects

[](#storing-and-retrieving-multiple-similar-objects)

You can store multiple objects of the same class using custom names:

```
$context = new Context();

$user = new User(12, "name@gmail.com");
$realUser = new User(1, "admin@gmail.com");

$context->set($user);                     // Stored with class name
$context->set($realUser, "realUser");      // Stored with custom name

// Retrieve the default user
$retrievedUser = $context->get(User::class);
var_dump($retrievedUser === $user); // true

// Retrieve with type enforcement
$retrievedUser = $context->getObject(User::class);
var_dump($retrievedUser === $user); // true

// Retrieve the real user by custom name
$retrievedRealUser = $context->get("realUser");
var_dump($retrievedRealUser === $realUser); // true

// Retrieve the real user with type enforcement
$retrievedRealUser = $context->getObject(User::class, "realUser");
var_dump($retrievedRealUser === $realUser); // true
```

### Storing and Retrieving Custom Data

[](#storing-and-retrieving-custom-data)

You can store and retrieve non-object values, such as arrays, strings, or numbers, using a custom name:

```
$context = new Context();

$userData = ["id" => 12, "email" => "name@gmail.com"];

// Store the array with a custom name
$context->set($userData, "user");

// Retrieve the stored array
$retrievedData = $context->get("user");
var_dump($retrievedData === $userData); // true
```

Working with References
-----------------------

[](#working-with-references)

### Working with Array References

[](#working-with-array-references)

When retrieving an array by reference, modifications to the retrieved array will affect the stored array.

```
$context = new Context();

$name = "original";
$original = ["foo" => "boo"];

$context->set($original, $name);

// Get a reference to the stored array
$ref = &$context->get($name);
$ref["foo"] = "changed";

// The original stored array is now modified
var_dump($context->get($name));
// Output: ["foo" => "changed"]
```

Fully Replacing an Array via Reference:

```
$name = "original";
$original = ["foo" => "boo"];

$context->set($original, $name);

// Get a reference to the stored array
$ref = &$context->get($name);
$ref = ["hello" => "world"];

// The original stored array is now modified
var_dump($context->get($name));
// Output: ["hello" => "world"]
```

### Working with Object References

[](#working-with-object-references)

hen retrieving an object, modifications to its properties affect the stored object.

```
$context = new Context();

$user = new User(1, "a@b.com");

$context->set($user);

// Get a reference to the stored object
$ref = $context->getObject(User::class);
$ref->id = 2;

// The original stored object is now modified
var_dump($context->getObject(User::class));
// Output: User { id: 2, email: "a@b.com" }
```

Error Handling
--------------

[](#error-handling)

### Type Enforcement Errors

[](#type-enforcement-errors)

If you try to retrieve an object with `getObject()`, but the stored data does not match the expected class, an exception is thrown:

```
$context = new Context();

// Store an array using the class name
$context->set(["id" => 12, "email" => "name@gmail.com"], User::class);

// This will throw an UnexpectedValueException because the stored data is not a User object
$context->getObject(User::class);
```

### Handling Missing

[](#handling-missing)

If you try to retrieve an object that has not been stored, an exception is thrown:

```
$context = new Context();

// This will throw an UnexpectedValueException because no User object exists
$context->getObject(User::class);

// This will throw an UnexpectedValueException because "randomName" does not exist too
$context->get("randomName");
```

###  Health Score

17

—

LowBetter than 8% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1215fe5ecc9ba0ab1c730d3c992125cf6ebf460562e66be71ebae127789d465a?d=identicon)[AntonPanfilov](/maintainers/AntonPanfilov)

---

Top Contributors

[![anton-panfilov](https://avatars.githubusercontent.com/u/1083546?v=4)](https://github.com/anton-panfilov "anton-panfilov (8 commits)")

### Embed Badge

![Health badge](/badges/ap-lib-context/health.svg)

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

###  Alternatives

[opengento/module-gdpr

Gdpr Compliance Module for Magento 2

14379.8k](/packages/opengento-module-gdpr)[redot/laravel-toastify

A simple package to use toastify.js in laravel applications

109.5k](/packages/redot-laravel-toastify)

PHPackages © 2026

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