Domanda

I am wondering is it possible to auto load static class, as like creating object to dynamically autoload library ? I have done most of the part of php autoloader but really need to tips to autoload static libraries for which I don't want to create object.

Is there anyone have solution ? Please post or else give me best idea to develop the same.

Thanks

È stato utile?

Soluzione

Yes SPL Autoloader will load classes and interfaces. Once the autoloader is triggered you can use any reference to an auto-loadable asset to trigger the load

My_Special_Class::SOME_CONSTANT

will trigger a load as well as calling or referencing any visible static method or property.

In fact exploiting this is one way to trigger the auto_loading of namespaced functions. Define a class file like this.

namespace My\Namespace;

abstract class Functions{
      const LOADED = true;
}

function func1(){}
function func2(){}
function func3(){}

And in your code when you need the functions defined in My\Namespace simply

if (\My\Namespace\Functions::LOADED){
    func1();
    func3();
}

The reference to the abstract class triggers the autoloader to include the file that defines the functions.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top