23

I slightly remember that autoload worked with the new statement. Now how about when I have several utility classes and I want to autoload these? And I only use static methods?

Like:

MathGuru::calculateFoo($bar);

Would autoload load MathGuru here? Or must I include it manually?

openfrog
  • 40,201
  • 65
  • 225
  • 373

6 Answers6

20

The autoloading mechanism works exactly the same way with static classes that it does with non-static one :

  • The autoload function/method you registered will be called
  • It'll receive the name of the class
  • And it'll have to require/include the necessary PHP code


Actually, the autoloader doesn't even have to "know" if it is called to load a static or a dynamic class, as its role is to load the PHP code that contains the class' definition -- and not instantiate it or anything.

James
  • 4,644
  • 5
  • 37
  • 48
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
6

Yes it would trigger __autoload.

It's not just 'new' that triggers autoloading - any reference to an unknown class will do it. Even something like using class_exists will trigger the autoloader (this isn't always desirable behaviour, which is why class_exists has a second parameter to disable autoloading)

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
5

Surely the best, and fastest way is to try it?

From the docs there is nothing that mentions new being needed.

Jake N
  • 10,535
  • 11
  • 66
  • 112
0

I had the exact same question as the original poster after autoloading of classes by calling their static methods didn't work. I was using Php 5.3.5 and came up with the following solution.

function autoload($class_name) {
    $directories = array(
        'php/classes/',
        'php/vendor/'
    );
    foreach ($directories as $directory) {
        if (file_exists($directory . $class_name . '.php')) {
            require_once($directory . $class_name . '.php');
            return;
        }       
    }
}
spl_autoload_register('autoload');

Note: the spl_autoload_register function has been used instead of __autoload which seems to have been the solution.

PetiPablo
  • 141
  • 1
  • 4
0

The goal of an autoloader is to load a class whenever it is needed. So, if your client code needs a class because you want to access a static class member, and it has not loaded yet, boom, it loads it and puts it in play.

Logically then, the first thing that should work are calls to static members. Using a constructor to instantiate an object typically happens after a class has loaded. So, the static case should be first in your heart (but there are too many books and such that under-utilize static class members). :-)

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44
0

I had one issue with this where a very minor syntax error gave a pretty unclear error message where it looked like __autoload() wasn't being called.

SomeClass:callStaticMethod(); // Doesn't call autoload for "SomeClass"

SomeClass::callStaticMethod(); // Successfully calls autoload for "SomeClass"

PHP strangely interprets the single ":" (instead of the correct double "::") as nothing. It treats callStaticMethod() as a global method call, meaning it skips the __autoload.

C.J.
  • 239
  • 1
  • 10
  • I know this is old but I want to kind of give a guess to what's going on here. I think it's going to interpret `SomeClass` as a reference to a global variable that does not exist, and the `:` as the start of a code block. Like if you were to go `foreach():`. – Anther Feb 26 '13 at 18:28
  • 1
    Actually, `:` is the PHP label symbol if used that way. So `SomeClass:callStaticMethod()` will be interpreted as a global function called `callStaticMethod` right after a label named `SomeClass`. Labels are used for the PHP `goto` keyword :) http://us2.php.net/goto – Michael Butler May 05 '16 at 20:03