0

PHP Autoloading Issue with Incorrect or Duplicate Namespaces in GLPI Project: Traits Not Found Due to Misconfigured PSR-4 Autoloading

I am working on a GLPI in and encountering persistent PHP errors related to incorrect or duplicate namespaces, such as:

PHP Fatal error: Trait 'Glpi\Glpi\Features\DCBreadcrumb' not found in C:\inetpub\wwwroot\glpi\src\DCRoom.php on line 41

I've noticed that somewhere in the project, an extra "Glpi" namespace is being prepended, resulting in namespaces like Glpi\Glpi\Features, which should ideally be just Glpi\Features. Despite various corrections, the issue persists, causing classes and traits to not be found correctly.

The Problem:

Despite these efforts, the errors persist, particularly those related to duplicated namespaces (e.g., Glpi\Glpi). This results in PHP failing to locate the correct traits and classes, severely impacting the functionality of the project.

Question:

How can I permanently resolve the autoloading issues with the duplicated Glpi\Glpi namespaces in my GLPI project? What steps or configurations should I check to ensure that PHP finds the correct traits and classes without the unnecessary namespace duplication?

What I Have Tried So Far:

  1. Regenerating Composer Autoload:

    • Ran composer dump-autoload -o to optimize and regenerate the autoload files.
  2. Reinstalling Dependencies:

    • Deleted the vendor directory and composer.lock file and reinstalled dependencies using composer install.
  3. Checking PSR-4 Autoloading Configuration:

    • Verified the psr-4 settings in composer.json to ensure correct namespace mappings:
      "autoload": {
          "psr-4": {
              "Glpi\\": "src/"
          }
      }
      
  4. Correcting Use Statements and Namespaces:

    • Updated use statements in affected files to match the intended namespace structure, e.g., use Glpi\Features\DCBreadcrumb;.
    • Despite these corrections, changing use statements sometimes resolves one error but introduces another, such as:
      PHP Fatal error: Uncaught Error: Class "DCRoom" not found in C:\inetpub\wwwroot\glpi\inc\define.php on line 636
      
  5. Searching for Duplicate Includes/Requires:

    • Performed extensive searches (grep) throughout the codebase to identify any duplicate or misconfigured includes/requires that might be contributing to the issue.
  6. Reviewing Directory Structure and Naming Conventions:

    • Ensured that the directory structure aligns with the PSR-4 autoloading configuration and that there are no stray folders or misnamed files causing the autoloader to prepend the extra "Glpi" namespace.
2
  • 1
    Try extracting a minimal reproducible example. At some point, it should become obvious what's wrong and if not, you can post that here. BTW, there is composer validate and composer diagnose which could give you a hint what's wrong. Commented Sep 2, 2024 at 13:29
  • PHP Fatal error: Uncaught Error: Class "DCRoom" not found in C:\inetpub\wwwroot\glpi\inc\define.php on line 636 what does this line actually look like? Show us Commented Sep 2, 2024 at 15:00

1 Answer 1

0

See: How does PSR-4 autoloading work in composer for custom libraries?

On your composer.json you have:

"autoload": {
    "psr-4": {
        "Glpi\\": "src/"
    }
}

This is saying that GLPI namespace is inside the root src folder; but your question shows "\inetpub\wwwroot\glpi\src\DCRoom.php" as the path which implies \inetpub\wwwroot is your project root folder.

So update your json;

"autoload": {
    "psr-4": {
        "Glpi\\": "glpi/src/"
    }
}

Should correct the default path on your composer.json for reaching your GLPI namespaced code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.