0
votes

I'm trying to understand how to use PSR-4 with Composer. I have 3 folders in my project: folder, folder2 and `vendor. Outside all these folders there's four files: index.php, composer.phar, composer.json and X.php. Some of these files are descibed below:

index.php

require_once 'vendor/autoload.php';

use \folder\in\A;
use \folder\in\B;
use \folder\C;
use \folder2\D;

$a = new A();
$b = new B();
$c = new C();
$d = new D();
$x = new X();

X.php

<?php

class X {
        public function __construct() {
            echo "Classe X";
        }
}

composer.json

{
    "autoload":{
        "psr-4": {
            "folder\\": "folder/",
            "folder2\\": "folder2/"
        }
    }
}

The index.php file is working well for the files that are in folder and folder2, but the X.php is not being found. What's the problem? How I can found the X.php file in index.php using Composer and PSR-4? I can found the X.php file using require_once, though, but I want to know how to this with Composer and PSR-4.

2
You are "using" all the classes but class XDale
Your X class lacks a namespace.fisk
What namespace I should use in class X? This class is outside all other folders.Vinicius
If you want to structure your code properly, no classes should be up in the root. What namespace and/or folder depends on what the class actually does.Magnus Eriksson

2 Answers

1
votes

You could use the autoload : files facility that comes with composer

https://getcomposer.org/doc/04-schema.md#files

{
    "autoload": {
        "files": ["X.php"]
    }
}

Personally I like to use this to load my own autoloader, so that I don't have to keep on adding files to composer.json (I'm just weird like that)

{
    "autoload": {
        "files": ["customLoader.php"]
    }
}

And my customLoader.php may be along the lines of

<?php

$files = ['X.php', 'Z.php']; // etc
foreach($files as $file) {
    require_once($file);
}
0
votes

If the class X is not inside a namespace, you cannot use PSR-4 for autoloading, but have to use PSR-0.

However, namespaces are the defacto standard today, and it's hard to find reasons not to put new code into namespaces.

Doing this means that the file location of X.php will change, the file will have to be moved somewhere appropriate according to the namespace.

Also note that PHP namespaces do not have anything to do with folders in the first place. Do use namespaces that make sense in a way that they are grouping your code into something meaningful. If your class X has nothing in common with your other classes, you still could add another namespace that demonstrates exactly this.

It also helps to have all classes in a project share the same main project namespace, or a vendor namespace. That way you only need to add one line to the PSR-4 autoloading, and this would also mean it is the namespace to be used for your X class.

Using PSR-4 then means that the files containing the classes have to be placed in folders with the same name and structure, but the folder name itself is the same as the namespace only by coincidence, not because use some\namespaced\class does anything with the folders that class file is located in.