PSR-4 directory structure and namespacing for a set of functions?
Asked Answered
S

2

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?

Shutdown answered 7/9, 2015 at 17:6 Comment(0)
C
10

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.

Crawley answered 7/9, 2015 at 17:14 Comment(2)
So, I would structure the directories thusly: my_lib/function1.php, and then namespace it like this: <?php namespace my_lib; function function1() {} ?Shutdown
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 github.com/php-fig/fig-standards/blob/master/accepted/…Crawley
P
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() {}
Panicle answered 29/8, 2018 at 23:53 Comment(5)
Is it really justified to create a class which has only a static function as a member, just because it's easier to autoload?Grecism
Is it really justified to create a file which has only a function as contents?Panicle
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.Grecism
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.Grecism
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.Grecism

© 2022 - 2024 — McMap. All rights reserved.