PHP code example of mistralys / variable-hasher

1. Go to this page and download the library: Download mistralys/variable-hasher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mistralys / variable-hasher example snippets


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();

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;
    }
}

use Mistralys\VariableHasher\VariableHasher;

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

assert($hash1 === $hash2);

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

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

use Mistralys\VariableHasher\VariableHasher;

$hash = VariableHasher::create('a', 'b', 'c')
    ->setAlgorithm('murmur3f') // 

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

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