How to use without Composer?
Mkayxxx opened this issue · 6 comments
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!
You should define audoload function compliant with PSR-4.
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
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
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;
}
});
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:
Just install what it needs :-)
https://www.php.net/manual/en/bc.installation.php
https://www.php.net/manual/en/gmp.installation.php
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.