6

I have a set of PHP functions that I find useful. I want to create a PSR-4 compliant repository for them, but the guides I have found (1,2,3) seem to talk only about classes for autoloading.

For instance, my files are as follows, with one function per file:

my_cool_function1.php
my_cool_function2.php
... etc.

How can I create a PSR-4 compliant library from them?

user151841
  • 17,377
  • 29
  • 109
  • 171

2 Answers2

9

The reason you're not able to find any documentation for PSR-4 autoloading files which aren't classes, that's because as the specification states - it's designed for autoloading classes.

Taken directly from the official specs:

This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

More specifically;

The term "class" refers to classes, interfaces, traits, and other similar structures.

A file with functions isn't really a similar structure.

To autoload those files, you'll need to autoload using files:

"autoload": {
    "files": [
        "src/my_cool_function1.php",
        "src/my_cool_function2.php"
    ],
    "psr-4": {
        "SomeNamespace\\": "src/YourNamespace/"
    }
}

You'll notice from this, that the psr-4 spec maps (usually) to a namespace.

Amo
  • 2,884
  • 5
  • 24
  • 46
  • So, I would structure the directories thusly: `my_lib/function1.php`, and then namespace it like this: ` – user151841 Sep 07 '15 at 17:31
  • Namespacing should follow PSR4 standards, usually something like ```VendorName\PackageName```. A file containing just functions (i.e. that isn't a class, or a similar structured file) wouldn't be namespaced. Check the examples https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md – Amo Sep 07 '15 at 18:18
1

Don't forget you can use static functions in classes so that PSR-4 will load them

class MyClass {
    public static my_cool_function1() {}
}

Then you can invoke them as just a normal function using the colon operator:

MyClass::my_cool_function1() {}
Jonathan
  • 6,741
  • 7
  • 52
  • 69
  • 2
    Is it really justified to create a class which has only a static function as a member, just because it's easier to autoload? – inwerpsel Jun 23 '20 at 11:47
  • 1
    Is it really justified to create a file which has only a function as contents? – Jonathan Aug 13 '20 at 02:15
  • Nothing prevents you from including more than one function in a single file and/or namespace. In fact that's what a lot of people end up doing if they do decide to write plain old functions in PHP. Because of a lack of something corresponding to the autoloader for functions, you have to ensure the function is loaded yourself and often each new file can be a pain or a risk to manage. Why don't you worry about a class being the only object in a file? Because you never had to worry about it. – inwerpsel Aug 13 '20 at 16:21
  • So you do have a point. I was more lamenting the fact that when you want to write a standalone function and make it easy to use everywhere in your code you need to put it in a class which is otherwise not used, in which case it does nothing more than what a namespace does. – inwerpsel Aug 13 '20 at 16:38
  • A lot of the time such functions are also just added to the class where they happened to be needed the first time, even though the logic of the function could be used anywhere, which reduces readability once you start using the function in other places and can be a pain to refactor. – inwerpsel Aug 13 '20 at 16:38