Questions tagged [autoload]

autoload is a language convention which allows missing classes or method dependencies to be loaded on-demand.

autoload is implemented differently among languages which include it:

  • Perl's AUTOLOAD allows you to dynamically interpret method calls.
  • PHP's __autoload loads classes. To handle missing methods, the __call() magic method may be used.
  • Ruby's autoload command loads modules.
1633 questions
276
votes
12 answers

Best way to load module/class from lib folder in Rails 3?

Since the latest Rails 3 release is not auto-loading modules and classes from lib anymore, what would be the best way to load them? From github: A few changes were done in this commit: Do not autoload code in *lib* for applications (now you need…
Vincent
  • 16,086
  • 18
  • 67
  • 73
233
votes
4 answers

What is Autoloading; How do you use spl_autoload, __autoload and spl_autoload_register?

I am learning advanced PHP standards and trying to implement new and useful methods. Earlier I was using __autoload just to escape including multiple files on each page, but recently I have seen a tip on __autoload manual spl_autoload_register()…
xkeshav
  • 53,360
  • 44
  • 177
  • 245
152
votes
12 answers

Load lib files in production

I've upgraded one of my apps from Rails 4.2.6 to Rails 5.0.0. The Upgrade Guide says, that the Autoload feature is now disabled in production by default. Now I always get an error on my production server since I load all lib files with autoload in…
Tobias
  • 4,523
  • 2
  • 20
  • 40
111
votes
13 answers

How do I use PHP namespaces with autoload?

I get this error when I try to use autoload and namespaces: Fatal error: Class 'Class1' not found in /usr/local/www/apache22/data/public/php5.3/test.php on line 10 Can anyone tell me what I am doing wrong? Here is my…
David Barnes
  • 2,138
  • 5
  • 19
  • 25
72
votes
5 answers

Why doesn't Rails autoload classes from app/services?

I'm working on a Rails 4.2 app and have just added app/services/fetch_artists.rb to the structure. Inside this file, I have defined a class FetchArtists; end. When trying to run rails r 'FetchArtists' it gives me a NameError: uninitialized constant…
linkyndy
  • 17,038
  • 20
  • 114
  • 194
72
votes
6 answers

Using Composer's Autoload

I have been looking around the net with no luck on this issue. I am using composer's autoload with this code in my composer.json: "autoload": { "psr-0": {"AppName": "src/"} } But I need to autoload at a higher level than the vendor…
Chris R
  • 1,263
  • 2
  • 10
  • 14
69
votes
5 answers

Autoloading classes in PHPUnit using Composer and autoload.php

I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test. I would like to have all my classes autoloaded into each unit test without having to use include or require at the…
Jasdeep Khalsa
  • 6,740
  • 8
  • 38
  • 58
67
votes
2 answers

Composer/PSR - How to autoload functions?

How can I autoload helper functions (outside of any class)? Can I specify in composer.json some kind of bootstrap file that should be loaded first?
mpen
  • 272,448
  • 266
  • 850
  • 1,236
59
votes
4 answers

What is autoload in php?

what is autoload in PHP?
DEVOPS
  • 18,190
  • 34
  • 95
  • 118
50
votes
1 answer

How to use spl_autoload() instead of __autoload()

According to http://php.net/manual/en/language.oop5.autoload.php the magic function __autoload() was deprecated as of PHP 7.2.0 and removed as of PHP 8.0.0. The official alternative is spl_autoload(). See…
Sliq
  • 15,937
  • 27
  • 110
  • 143
46
votes
12 answers

Autoload classes from different folders

This is how I autoload all the classes in my controllers folder, # auto load controller classes function __autoload($class_name) { $filename = 'class_'.strtolower($class_name).'.php'; $file =…
Run
  • 54,938
  • 169
  • 450
  • 748
38
votes
1 answer

Difference between PSR-4 and classmap autoloading?

In regards to Laravel, I got a question about Composer autoloading i.e. the difference between "psr-4" and "classmap" autoloading. 1 difference that I know is PSR-4 does not need repeated dumpautoload for every changes or addition of new files…
RoMo
  • 413
  • 1
  • 6
  • 10
37
votes
3 answers

Instantiating class by string using PHP 5.3 namespaces

I can't get around an issue instantiating a new class by using a string variable and PHP 5.3. namespaces. For example, this works; $class = 'Reflection'; $object = new $class(); However, this does not; $class = '\Application\Log\MyClass'; $object =…
Kevin
  • 958
  • 2
  • 11
  • 19
34
votes
1 answer

PHP adding custom namespace using autoloader from composer

Here is my folder structure: Classes - CronJobs - Weather - WeatherSite.php I want to load WeatherSite class from my script. Im using composer with autoload: $loader = include(LIBRARY…
John
  • 9,840
  • 26
  • 91
  • 137
32
votes
7 answers

Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?

I understand that you can use either a PSR standard to locate files, or tell composer a directory to scan for classes. The documentation recommends using the PSR-4 standard. There is also an option for composer to create an optimized autoloader,…
Bryan Agee
  • 4,924
  • 3
  • 26
  • 42
1
2 3
99 100