PHPackages                             mistralys/variable-hasher - 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. [Caching](/categories/caching)
4. /
5. mistralys/variable-hasher

ActiveLibrary[Caching](/categories/caching)

mistralys/variable-hasher
=========================

PHP utility that can be used to create a hash of an arbitrary set of variables.

1.0.0(10mo ago)0136↓87.5%1MITPHPPHP &gt;=8.4

Since Sep 2Pushed 10mo agoCompare

[ Source](https://github.com/Mistralys/variable-hasher)[ Packagist](https://packagist.org/packages/mistralys/variable-hasher)[ Docs](https://github.com/Mistralys/variable-hasher)[ RSS](/packages/mistralys-variable-hasher/feed)WikiDiscussions main Synced today

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

Variable hasher
===============

[](#variable-hasher)

PHP utility that can be used to create a hash of an arbitrary set of variables to use for runtime caching. It has better performance than serializing the variables and hashing the result, especially when objects and resources are involved.

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

[](#requirements)

- PHP 8.4 or higher
- [Composer](https://getcomposer.org/) for installation

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

[](#installation)

Use Composer to add the dependency to your project:

```
composer require mistralys/variable-hasher
```

Quick start
-----------

[](#quick-start)

Any kind of variable can be passed in, from primitives (int, float, string) to objects and resources, or arrays containing any of these - indexed or associative.

```
use Mistralys\VariableHasher\VariableHasher;

$intHash = VariableHasher::create(123)->getHash();

$multiHash = VariableHasher::create(123, 'string', 45.67, new stdClass())->getHash();

$array = array(
    'key' => 'value',
    'another_key' => 123,
    'object' => new stdClass()
);

$arrayHash = VariableHasher::create($array)->getHash();
```

Real-life example
-----------------

[](#real-life-example)

Consider a factory method that creates objects based on a set of parameters. The same instances must be returned if the same parameters are passed in again.

```
use Mistralys\VariableHasher\VariableHasher;

class MyObjectFactory
{
    private array $instances = array();

    public function create(array $params) : MyObject
    {
        $hash = VariableHasher::create($params)->getHash();

        if(isset($this->instances[$hash])) {
            return $this->instances[$hash];
        }

        $instance = new MyObject($params);
        $this->instances[$hash] = $instance;

        return $instance;
    }
}
```

Order-independence
------------------

[](#order-independence)

The order of the variables you pass in does not matter. The same hash will be generated regardless of the order if the variables are the same.

To illustrate, the following will generate the same hash:

```
use Mistralys\VariableHasher\VariableHasher;

$hash1 = VariableHasher::create('a', 'b', 'c')->getHash();
$hash2 = VariableHasher::create('c', 'b', 'a')->getHash();

assert($hash1 === $hash2);
```

Choosing the hash algorithm
---------------------------

[](#choosing-the-hash-algorithm)

By default, MD5 is used to generate the hash. If you need a different algorithm, you can specify any valid algorithm supported by PHP's `hash()` function.

```
use Mistralys\VariableHasher\HashingAlgorithms;
use Mistralys\VariableHasher\VariableHasher;

$hash = VariableHasher::create('a', 'b', 'c')
    ->setAlgorithm(HashingAlgorithms::HASH_SHA3_256)
    ->getHash();
```

In the example above, the algorithm is set using one of the provided constants. You can specify any valid algorithm as a string:

```
use Mistralys\VariableHasher\VariableHasher;

$hash = VariableHasher::create('a', 'b', 'c')
    ->setAlgorithm('murmur3f') // requires `php-murmurhash` extension
    ->getHash();
```

> NOTE: The `HashingAlgorithms` class is provided for convenience. It contains constants for all algorithms that you can rely on being supported by PHP 8.4 without having to check `hash_algos()`.

Get the raw hashing string
--------------------------

[](#get-the-raw-hashing-string)

If you want to see the raw string that is hashed, you can set the algorithm to none, which will return the string instead of a hash.

```
use Mistralys\VariableHasher\HashingAlgorithms;
use Mistralys\VariableHasher\VariableHasher;

$rawString = VariableHasher::create('a', 'b', 'c')
    ->setAlgorithm(HashingAlgorithms::HASH_NONE)
    ->getHash();
```

Memory handling
---------------

[](#memory-handling)

The arguments are stored internally until you request the hash. This means that if you pass in large variables, they will remain in memory until the object is destroyed if you do not request it.

Warning: Runtime use only
-------------------------

[](#warning-runtime-use-only)

The hashing uses object and resource IDs for performance reasons, which are not persistent between requests. As a result, the generated hashes cannot be used to identify a set of variables if it contains objects or resources.

If you need a persistent hash, you should use a different method, such as encoding the objects to JSON and generating a hash of this serialized data.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance54

Moderate activity, may be stable

Popularity13

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity53

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

Unknown

Total

1

Last Release

305d ago

### Community

Maintainers

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

---

Top Contributors

[![Mistralys](https://avatars.githubusercontent.com/u/8895528?v=4)](https://github.com/Mistralys "Mistralys (11 commits)")

---

Tags

cachinghashingphp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mistralys-variable-hasher/health.svg)

```
[![Health](https://phpackages.com/badges/mistralys-variable-hasher/health.svg)](https://phpackages.com/packages/mistralys-variable-hasher)
```

PHPackages © 2026

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