How manage annoying Namespace and Class name redundancy on PHP and PSR-4

249 Views Asked by At

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)

1

There are 1 best solutions below

0
On

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;