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.26k stars 417 forks source link

How to use without Composer? #186

Closed Mkayxxx closed 1 year ago

Mkayxxx commented 1 year ago

Hello, i do not use Composer for my project. Tried to put all the src files into /hashids/ directory within my project and simply use:

require_once($G_hidden_path . 'hashids/Hashids.php');
$hashids = new Hashids\Hashids('myCode');

but unfortunately that's not working. Please help!? Thank you!

eugene-borovov commented 1 year ago

You should define audoload function compliant with PSR-4. https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md

Mkayxxx commented 1 year ago

Sorry, i do not have any prior experience with autoloaders, could you provide a simple code to start using your library? From the site you showed, there's some code, no idea what to do with it

eugene-borovov commented 1 year ago

Just include it in very top of your code. Configure LIBS_PATH on your own.

<?php
define('LIBS_PATH', $G_hidden_path);
spl_autoload_register(function ($class) {
    $prefix = 'Hashids\\';
    $base_dir = LIBS_PATH . 'hashids/';

    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // no, move to the next registered autoloader
        return;
    }
    $relative_class = substr($class, $len);

    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    if (file_exists($file)) {
        require $file;
    }
});
Mkayxxx commented 1 year ago

I have copied the content of src folder into the $G_hidden_path/hashids I have used your suggested code:

define('LIBS_PATH', $G_hidden_path);
spl_autoload_register(function ($class) {
    $prefix = 'Hashids\\';
    $base_dir = LIBS_PATH . 'hashids/';

    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // no, move to the next registered autoloader
        return;
    }
    $relative_class = substr($class, $len);

    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    if (file_exists($file)) {
        require $file;
    }
});

use Hashids\Hashids;
$hashids = new Hashids('myCode');

However, it still gives an error : Fatal error: Uncaught RuntimeException: Missing BC Math or GMP extension. in /var/www/hiddenstuff/hashids/Hashids.php:408 Stack trace:

eugene-borovov commented 1 year ago

Just install what it needs :-) https://www.php.net/manual/en/bc.installation.php https://www.php.net/manual/en/gmp.installation.php

Mkayxxx commented 1 year ago

Uff, simple things made so hard. I have used your previous version for ages. That was until some dynamic loading got depreciated and it started to throw errors starting with php 8. So decided to upgrade and now i find all this dance to be danced before having a working function. But thank you so much for this valuable function, very very useful, just wish the installation would be a simple one folder include, no extra hassles. Thanks again, will go and see how can i install those two addons.