vinkla / hashids

A small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.
https://hashids.org/php
MIT License
5.28k stars 418 forks source link

decode() produces empty array #130

Closed timgavin closed 5 years ago

timgavin commented 5 years ago

Using Laravel 5.8 and PHP 7.3.3

Encoding works fine, but decoding produces an empty array []

public static function encodeHashId($id)
{
    $hashids = new Hashids('', 8, 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNOPQRSTUVWXYZ');

    return $hashids->encode($id);
}

public static function decodeHashId($id)
{
    $hashids = new Hashids();

    return $hashids->decode($id);
}

$id = 'ZGkbkrdn';

$id = static::decodeHashId($id);

dd($id);

// produces [] instead of 1
roukmoute commented 5 years ago

This is normal, you do not use the same instanciation. Use the same instanciation and it will works. Next, to avoid this kind of error, use dependency injection.