For those who spent a lot of time and did not find a clue, tearing his hair and hitting the wall to death:
- you need to have an autoloader, like in "bootstrap.php" in the "test /" folder, to manage each namespace ... or you need to link each file in a folder that is not very smart. See
spl_autoload_register at spl_autoload_register - you just downloaded RandomLib, which depends on another library (the author did not mention this): you need SecurityLib (it has the same folder structure: copy what is inside "lib /" to another folder "lib /".
an example of an autoloader for calling a script from the root folder "RandomLib-1.1.0 /" (see 'lib' in $path ?):
spl_autoload_register(function ($class) { $nslen = strlen(__NAMESPACE__); if (substr($class, 0, $nslen) != __NAMESPACE__) { //Only autoload libraries from this package return; } $path = substr(str_replace('\\', '/', $class), $nslen); $path = __DIR__ . '/lib/' . $path . '.php'; if (file_exists($path)) { require_once $path; } });
you are now set up and you can freely use classes without worrying about including or requesting files.
$factory = new RandomLib\Factory; $generator = $factory->getLowStrengthGenerator(); //$generator = $factory->getMediumStrengthGenerator(); //$generator = $factory->getHighStrengthGenerator(); $randomStringLength = 16; $randomStringAlphabet = ' 0123456789@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ +/'; $randomString = ''; for ($i=0;$i<10;$i++){ $randomString = $generator->generateString( $randomStringLength , $randomStringAlphabet); echo $randomString.'<br>'; }
source share