4

I have

"autoload": {
      "psr-4": {
          "ACME": "src/",
      },
      "classmap": ["src/"],
      "files": ["mapper.php"],
      "exclude-from-classmap": ["mapper.php"]
  },

in mapper.php I'm trying to give different namespaces for some legacy stuff.

<?php
class_alias(Some_Class::class, 'Cool\NameSpaced\Class');

I think this fails to build because mapper.php is using classes in src/ and they have not been loaded by composer yet. Is there a way to do this?

Command that I run is composer install --optimize-autoloader --no-dev

Thellimist
  • 3,757
  • 5
  • 31
  • 49

1 Answers1

4

I've tried you example and it works well.

It might be related to composer command you use. Try this one instead:

composer dump-autoload

This will refresh anything from autoload section.


Here is the setup file by file:

composer.json

{
    "autoload": {
        "classmap": ["src/"],
        "files": ["mapper.php"]
    }
}


mapper.php

<?php

class_alias(Some_Class::class, 'Cool\NameSpaced\Class');


index.php

<?php 

require __DIR__ . '/vendor/autoload.php';

var_dump(class_exists(Some_Class::class));
var_dump(class_exists('Cool\NameSpaced\Class'));


Test in CLI

$ composer dump-autoload
$ php index.php
bool(true);
bool(true);

How to find out aliasing works?

commposer.json

{
    "autoload": {
        "classmap": ["src/"]
    }
}


Test in CLI

$ composer dump-autoload
$ php index.php
bool(true);
bool(false);
Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115