Autoloading
Last updated
What is Autoloading?
Autoloading is a convenient feature in PHP that allows for automated loading of classes or files when they are needed. For example, if you have a class that is not yet included or required in the script, and you're instantiating that class, PHP would automatically include that class file for you, given that it complies with the standard PHP autoloading conventions. This feature cuts down the need to manually include or require files and makes your code cleaner and more efficient to manage. Here's a simple example:
spl_autoload_register(function ($class_name) {
include $class_name . '.php';
});
$obj = new MyClass();
In this example, PHP will automatically load the MyClass.php file when the MyClass is instantiated.
Free resources to learn Autoloading
- Official docsAutoloading
More in stage 12: PHP-FIG
Autoloading: common questions
Can I learn Autoloading in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Autoloading in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this Autoloading tutorial for beginners?
It is lesson 95 of 120 in the free PHP roadmap, in stage 12, PHP-FIG. If you are new to PHP, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.