ChatGPT解决这个技术问题 Extra ChatGPT

Composer/PSR - How to autoload functions?

How can I autoload helper functions (outside of any class)? Can I specify in composer.json some kind of bootstrap file that should be loaded first?


J
Jeff Puckett

You can autoload specific files by editing your composer.json file like this:

"autoload": {
    "files": ["src/helpers.php"]
}

(thanks Kint)


Note that this file will always be included when calling the autoload file - so this better be quick, or every php script using it will be affected.
This was helpful, thanks. Just as a recomendtion, check if the function exist first : if (!function_exists('myfunction'))
How do I load a functions.php file containing only functions from a Git repo in a package? I have access to edit the the package's composer.json file.
@DonnieAshok Not sure what Git has to do with it. You can't load functions remotely if that's what you're asking. Just change src/helpers.php to path/to/functions.php. If that's not clear, you might want to ask a new question.
This isn't a suitable answer for people who have a PSR-4 autoload pattern already set up and want to autoload functions in the same manner as classes.
A
Anwar

After some tests, I have came to the conclusions that adding a namespace to a file that contains functions, and setting up composer to autoload this file seems to not load this function across all the files that require the autoload path.

To synthesize, this will autoload your function everywhere:

composer.json

"autoload": {
    "files": [
        "src/greetings.php"
    ]
}

src/greetings.php

<?php
    if( ! function_exists('greetings') ) {
        function greetings(string $firstname): string {
            return "Howdy $firstname!";
        }
    }
?>

...

But this will not load your function in every require of autoload:

composer.json

"autoload": {
    "files": [
        "src/greetings.php"
    ]
}

src/greetings.php

<?php
    namespace You;

    if( ! function_exists('greetings') ) {
        function greetings(string $firstname): string {
            return "Howdy $firstname!";
        }
    }
?>

And you would call your function using use function ...; like following:

example/example-1.php

<?php
    require( __DIR__ . '/../vendor/autoload.php' );

    use function You\greetings;

    greetings('Mark'); // "Howdy Mark!"
?>

That is because your condition is incorrect - you should use FQN for function_exists(): if(!function_exists('You\greetings')). It has nothing to do with autloading.
You can also use if (! function_exists(__NAMESPACE__ . '\greetings')) to avoid repeating the FQDN too many times, which makes it easier to move namespaces if necessary during development.
+1 for working example, also if you don't want to specify namespace before each function call just add use function You\greetings; to the top of you file
i
iamandrewluca

Add autoload info in composer.json

{
    "autoload": {
        "psr-4": {
            "Vendor\\Namespace\\": "src/"
        }
    }
}

Create a OwnFunctions.php with your functions in src/Functions folder

// recommend
// http://php.net/manual/en/control-structures.declare.php
declare(strict_types=1);

namespace Vendor\Namespace\Functions\OwnFunctions;

function magic(int $number): string {
    return strval($number);
}

In your index.php require composer autoload

declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';

use function Vendor\Namespace\Functions\OwnFunctions\magic;

echo magic(1);

// or you can use only OwnFunctions namespace
use Vendor\Namespace\Functions\OwnFunctions;

echo OwnFunctions\magic(1);

This also can be done with const

use const Vendor\Namespace\Functions\OwnFunctions\someConst;
echo someConst;

Official docs


This will not autoload anything - functions cannot be autoloaded in PHP, autoloading works only for classes.
You are right. This will work only if including files manually, or multiple namespaces in same file.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now