Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Autoloading a package
  • I must be missing something, because I cannot seem to access a package unless it is added to the "always_load" array. I create a package "Foo", add a class "Foo" with a static method "bar". Then I create the bootstrap file. From a controller, calling up Foo::bar() gives me the error: ErrorException [ Error ]: Class 'Foo' not found. If I add "foo" to the always_load packages list, then calling Foo::bar() from a controller works fine. This isn't right, is it? Foo should auto-load just by calling it up, should it not? Or perhaps static methods don't autoload, and so I need to instantiate the class as an object some other way? -- JJ
  • Use
    Package::load('foo');
    
    then call
    Foo\Foo::bar
    
  • The autoloader doesn't know packages that have not been loaded. For performance reasons it doesn't scan your filesystem to see if it can find something that matches what you are calling, instead you have to tell it ( through the bootstrap file ) what is loadable and what not. And the bootstrap is processed when you load the package ( or when it is "always loaded" ).
  • I see - that makes sense. Adding a package to be autoloaded just tells FuelPHP about the bootstrap, so it has a list of what is available for loading on demand in that package. It does not go any further than that at the bootstrap stage (unless additional code to do some initialisation has been added to bootstrap)? So loading a library package on every page is not [generally] a massive overhead. Thanks. I'll use a mix of the two approaches - both always_load and Package::load, depending on where and how the package is used. The documentation that I didn't find, is here: http://fuelphp.com/dev-docs/classes/package.html but it does not look like it is mentioned in the main documentation section (unless I'm just not seeing it): http://fuelphp.com/docs/general/packages.html -- Jason
  • Correct. The only overhead multiple loads introduce is related to the call itself. The load() method will detect that the requested package is already loaded, and will terminate immediately. The package class is a new addition, so not present in the official docs (which are for the last official release, 1.0.1).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion