Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Autoloading Functions and Constants #6232

Closed
PabloJoan opened this issue Mar 7, 2017 · 9 comments
Closed

Support Autoloading Functions and Constants #6232

PabloJoan opened this issue Mar 7, 2017 · 9 comments

Comments

@PabloJoan
Copy link

PabloJoan commented Mar 7, 2017

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?

@robfrawley
Copy link

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

@PabloJoan
Copy link
Author

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.

@Seldaek
Copy link
Member

Seldaek commented Mar 8, 2017

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.

@Seldaek Seldaek closed this as completed Mar 8, 2017
@crux
Copy link

crux commented Jun 3, 2022

this lack of autoloading function files is imposing Object Oriented mupitz on all of us. A function is a function and NOT! a class. It really is a sympthon of bad education of all these people who think putting pure stateless functions in a class is in any way a good idea. It totally sucks, and degrades software quality imposing OO mumpitz in places where it doesnt belong.

@GromNaN
Copy link
Contributor

GromNaN commented Jun 4, 2022

Composer has "file autoloading" that is used for functions and constants.
https://getcomposer.org/doc/04-schema.md#files

Example with Symfony polyfills
https://github.com/symfony/polyfill/blob/main/src/Php82/composer.json

@crux
Copy link

crux commented Jun 4, 2022

no, it does not. It has the "files" feature to autoload PHP files. But neither declaration of the function:

use My\Namespace\Functions\foo;

nor its usage:

foo(1,2,3);

will trigger the autoloading of the file containing the function.

Explicitly pre-loading all files is quite different from autoloading files on demand.

@Seldaek
Copy link
Member

Seldaek commented Jun 4, 2022

Please direct your php language feature requests/rants to the appropriate channels. There's nothing we can do about this here.

@crux
Copy link

crux commented Jun 9, 2022

Sorry when I was hurting your or anyone's feelings, but rejecting false statements is okay in a technical discussion?

Composer has "file autoloading" << , is best of case misleading, and in imho just plain wrong.

@charlesmulder
Copy link

I would also like to see this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants