5

I am getting crazy.

Is there any way to use composer's autoload functionality to load a file without (!) classes, just containing a namespace with functions, but dynamically?

Imagine a file 1 (Foo.php) in a library which I put into a private composer repository:

namespace Foo;

function a() {
//...
}

and a file 2 using this library via composer again:

require(.../autoload.php);

\Foo\a();

I want composer to generate autoload functionality to load the "Foo.php" only if I call a function etc. from it.

But I do not want to wrap Foo.php into a class.

Schubi Duah
  • 309
  • 1
  • 7
  • Possible duplicate of [Composer/PSR - How to autoload functions?](https://stackoverflow.com/questions/24171078/composer-psr-how-to-autoload-functions) – rob006 Sep 14 '18 at 08:27
  • 1
    Not really. I want to have the same functionality as with classes: Only load the php file if something from it will be used! – Schubi Duah Sep 14 '18 at 08:36
  • 1
    Currently there is no way to implement autoloading for functions or constants in PHP. This is the best what you can get. – rob006 Sep 14 '18 at 08:47

1 Answers1

0

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.

ADJenks
  • 2,973
  • 27
  • 38