Your desire to autoload functions is logical because may not want to be constrained to OOP, but autoloading could still be efficient.
I believe the problem lies in the built-in PHP autoloader mechanism itself. As @rob006 said, "there is no way to implement autoloading for functions or constants in PHP", but I wanted to see why for myself.
I ran some quick tests and discovered that the spl_autoload_register() function will not call the callback function when an unknown function is called.
Set up the experiment:
php > spl_autoload_register(function($class_name){
php ( echo 'Triggered for: '.$class_name;
php ( });
Calling a namespaced function does not trigger the echo:
php > \test\test();
PHP Warning: Uncaught Error: Call to undefined function test\test() in php shell code:1
Calling the new
operator does trigger the echo:
php > $test = new \test\test();
Triggered for: test\test
PHP Warning: Uncaught Error: Class 'test\test' not found in php shell code:1
Calling a static function does trigger the echo:
php > \asdf\adsf::asdf();
Triggered for: asdf\adsf
PHP Warning: Uncaught Error: Class 'asdf\adsf' not found in php shell code:1
use
does not trigger anything:
php > use \test4\test4;
So as you can see, it would be difficult to implement in composer because the built-in autoloader in PHP does not make any effort to load functions. The best we can do is to make a wrapper class full of static functions. This is what I did before but I wanted to double check if there was a real solution or an edit that could be made to composer itself.
I did find this interesting Aspect Oriented Programming framework along the way called GoAOP. It seems to be able to intercept functions but I think it does it by rewriting the code so I don't think it's really an ideal solution to this problem. It was however the only method I could find to intercept functions.
Perhaps we can get function load interception into PHP 9 if we try.