-1

I have different versions of files which are stored in separate subfolders inside a main folder. I intend to use subfolder based on a dynamically determined value of $ver.
Can I create a psr-4 composer namespace for the main folder and then add to it the $ver for subfolders as needed.
I tried following but its showing errors.

File Structure:

web\  
  public\index.php 
  main\
        V1\app.php
        V2\app.php
        V3\app.php

Composer:

    "autoload": {
        "psr-4": {
            "Application\\": "main"
        }
    }

index.php:

use Application;

$ver = "V2"; // suppose its V2 for now

$app = new "\Application\" . $ver . "\App()";

app.php:

namespace Application;  
class App {

}

Crunch
  • 57
  • 12
  • But why? Use factory pattern: AppFactory::createApp(string $version): AppInterface – Maksim Aug 09 '21 at 10:12
  • 2
    main/V1/App.php => namespace Application\V1; class App {} – Maksim Aug 09 '21 at 10:38
  • Still this line shows error: ```$app = new "\Application\" . $ver . "\App()";``` – Crunch Aug 09 '21 at 11:03
  • What do you mean by "its showing errors"? Can you share these errors, along with your attempts to resolve them? Also, is this even related to Composer? – Nico Haase Aug 09 '21 at 11:23
  • Main question was about Composer & Namespaces but my above comments are unrelated. Following accepted answer solves all issues. I don't know why this question got a down vote, this black box behavior should have some trail. – Crunch Aug 09 '21 at 11:31
  • What do you mean by "black box behavior"? – Nico Haase Aug 09 '21 at 11:52
  • How is that link related to your question? – Nico Haase Aug 09 '21 at 11:59
  • I just answered your question. – Crunch Aug 09 '21 at 12:02
  • "this black box behavior should have some trail" - what do you mean by this? Which parts of your code, or of the way it is handled, is a black box for you? – Nico Haase Aug 09 '21 at 12:31
  • As an improvement idea: you mentioned in the comment section that you are facing any error, but still haven't shared that error message - just add it to your question initially, such that others can see where the code is going wrong. Also, check whether the tags are really matching the problem. I still don't see any connection to Composer in your question: there are no packages shared, no `composer.json`, no call of `composer` that indicates that you want to install anything. All this helps to avoid downvotes (and no, I haven't downvoted your question) – Nico Haase Aug 09 '21 at 14:23
  • Thanks for your time. The question is about composer & namespaces. I was using ```namespace Application;``` in ```app.php``` file and my composer file had the entry "autoload": { "psr-4": { "Application\\": "main" } } . These all facts are mentioned in my question. Upon execution there were errors: "Unable to find file". I was not sure where the error was, in namespace or the composer entry. But selected answer guided me to use ```namespace Application\V1;``` in ```app.php``` file. – Crunch Aug 09 '21 at 15:30

1 Answers1

1

You must follow PSR-4 for class naming and app structure:

composer.json
    "autoload": {
        "psr-4": {
            "Application\\": "main"
        }
    }
main/V1/App.php
    namespace Application\V1;
    class App {}

For instance class with dynamic name use next construction:

$ver = "V2"; // suppose its V2 for now
$appClass = "\Application\$ver\App";
$app = new $appClass;

Read more about this here

And i recommend to use factory pattern for this case:

class App implements AppInterface {}
class AppFactory
{
    public static function makeApp(string $version): AppInterface
    {
         $appClass = "\Application\$version\App";
         if(! class_exists($appClass){
             throw new Exception("App version $version not exists");
         }
         return new $appClass;
    }
}
Maksim
  • 2,653
  • 2
  • 13
  • 28