Composer: Support Autoloading Functions and Constants

Created on 7 Mar 2017  ·  3Comments  ·  Source: composer/composer

I have this composer package https://github.com/PabloJoan/slack-delete

I do not use any classes to write code. Only functions. I organize my code by putting 1 function in 1 file, organizing these files in folders, and then name space them. Basically following everything in PSR-4 except instead of a Class with namespace, its a Function with namespace.

there is no autoloading feature in composer to support this.

I've had to resort to using "files" to get this to work https://getcomposer.org/doc/04-schema.md#files .
The issue is that I have to manually add all these files and it can get very lengthy.

See below.
My composer.json:

{
    "autoload": {
        "files": [
            "src/APIURL.php",
            "src/Cli/getToken.php",
            "src/Cli/getUser.php",
            "src/Delete/all.php",
            "src/Delete/channels.php",
            "src/Delete/files.php",
            "src/Delete/groups.php",
            "src/Delete/history.php",
            "src/Delete/ims.php",
            "src/User/get.php"
        ]
    }
}

How would you suggest I manage this better?

Most helpful comment

PHP itself offers no autoloading of functions/constants, so until it does we can't really support that at all. Wrapping in classes definitely is easier, even though it feels a bit silly/redundant that the language makes us do that.

All 3 comments

Out of pure curiosity, why would you do this? Moving to classes will help with your own sanity as the project expands.

You might be right. This is an experiment. eventually I would like to see how to implement functional programming in to PHP and seamlessly be part of the php community with composer. I know the sample project I have is not really "functional" right now, but composer supporting autoloaded functions with namespaces will be a huge leap forward.

It follows the same general namespacing rules as the PSR4 for classes, so its not intrusive to existing codebases.

PHP itself offers no autoloading of functions/constants, so until it does we can't really support that at all. Wrapping in classes definitely is easier, even though it feels a bit silly/redundant that the language makes us do that.

Was this page helpful?
0 / 5 - 0 ratings