How to implement a class using namespaces in PHP

I had a problem implementing a particular class in PHP. I want to use the RandomLib Anthony Ferrara library in my Zend Framework application (you can find here .)

I have been programming in PHP for several years, so I know how to do this for the most part. But I have to admit that I don’t know a bit when it comes to using classes that implement namespaces. Here is what I got in my code:

public static function generateToken() { require_once 'RandomLib/Factory.php'; $factory = new \RandomLib\Factory; $generator = $factory->getMediumStrengthGenerator(); return $generator->generate(16); } 

Unfortunately, I get the following error:

 Fatal error: Class 'SecurityLib\AbstractFactory' not found in C:\xampp\php\includes\RandomLib\Factory.php on line 30 

As I said, I really have no idea what is going on here. I don't know if I should use any use statement in my class or not.

+4
source share
3 answers

With an autoloader with ZF 1. *, if you add your factoru to program_name / libs / RandomLibFactory.php as a class RandomLibFactory, it should look like this:

 public static function generateToken() { $factory = $locator = new RandomLibFactory(); $generator = $factory->getMediumStrengthGenerator(); return $generator->generate(16); } 
+1
source

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>'; } 
0
source

I also pulled out a lot of hair before I found a solution for my Windows machine.

  • I downloaded the version of Windows Composer (which I barely heard about that I hadn’t used).
  • I used it to install RandomLib (as shown in the "Installation" section of the instructions - use the command window on Windows)
  • This created a vendor folder containing the composer folder, the ircmaxell folder, and the autoload.php file; they are uploaded to a suitable location on my website. The vendor directory includes the random-lib and-required-security-lib libraries.
  • In the program in which I wanted to generate random text, I included autoload.php (which was preceded by the corresponding directory pointers)
  • Then it was useful for me to move on to creating a factory, a generator and a string, as shown in the library instructions
0
source

Source: https://habr.com/ru/post/1484805/


All Articles