-1

In many of my PHP micro-library, there are Class matching with project name, it create a strange fully qualified class name, look at this

<?php
namespace Javanile\MysqlImport;

class MysqlImport {
 ...
}

Then when I use this class, I have the annoying double name repetition

<?php

$importer = new \Javanile\MysqlImport\MysqlImport(...);

// OR (EVEN BAD)

use Javanile\MysqlImport\MysqlImport;
$importer = new MysqlImport(...);

According to PSR-4 we have:

  • Javanile: is the vendor name
  • MysqlImport: is the subpackage name it fixes the project name
  • MysqlImport: is the principal class of the library

Please support me to handle the annoying double repetition of the project and principal class name on the full qualified class name. It is so bad, what's the right move? (I don't want to come up with random class names just to avoid it)

Francesco Bianco
  • 519
  • 1
  • 5
  • 12
  • What's wrong with the `use` statement, exactly? Most editors will just add them for you anyway. You can also alias the class if you don't like its name. – Jeto Oct 14 '20 at 22:59
  • No syntax wrong, it noisy for me, using alias don't remove my mess, the problem is MysqlImport are twice in full class name, one by project name and one for the class it self – Francesco Bianco Oct 14 '20 at 23:01
  • 1
    I'm not sure what you're asking. If that's your library, you can change the class name itself or its namespace/location as desired. If that's not your library, you don't really have a choice: use its fully qualified name (usually not recommended), import it normally, or alias it. No one reads `use` statements anyway, it's not like your code will become hard to read or something because of it. – Jeto Oct 14 '20 at 23:04
  • I hate when in a PHP library there a class with the same name as the project – Francesco Bianco Oct 14 '20 at 23:07

1 Answers1

0

This is not wrong, but if you want more clean project, you can some reorganize your project structure. For example:

Javanile
|__Importers
   |__ Mysql.php <-- Mysql class with main logic
   |__ Mysql <-- Folder with special MySQL logic, used in Mysql.php
      |__ MysqlHelperThings.php
      |__ SomethingElseForMysql.php

With this structure your use path will look as use Javanile\Importers\Mysql;