← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 8 of 24

PHP Autoloading

Autoloading एक librarian जैसा है जो एक book सिर्फ उस moment लाता है जब आपको ज़रूरत हो। PHP जब आपका code पहली बार किसी class इस्तेमाल करता है तो automatically उस class file को load करता है।
Syntax
php
spl_autoload_register(function ($class) {
    require __DIR__ . "/" . $class . ".php";   // one class per file
});

$object = new ClassName();   // file loaded automatically

Autoloading क्या है?

Autoloading PHP को पहली बार जब कोई class actually इस्तेमाल हो तब automatically उसकी class file load करने देता है, हर file के top पर पुराने codebases को चाहिए होने वाले tedious block of require statements को खत्म करते हुए।

उदाहरण: What is Autoloading?

php
<?php
// Without autoloading: require 'User.php'; require 'Product.php'; ...
echo "Autoloading loads a class file automatically the first time it's used";
?>

spl_autoload_register() Function

spl_autoload_register() एक callback function register करता है जिसे PHP जब भी code किसी अभी तक load न हुई class को reference करता है invoke करता है, आपको पूरा control देते हुए कि class names file paths पर कैसे map होते हैं।

उदाहरण: The spl_autoload_register() function

php
<?php
spl_autoload_register(function ($class) {
    // Print "Would load file for class: $class\n" to the output
    echo "Would load file for class: $class\n";
});
// Call `class_exists("SomeUndefinedClass")`
class_exists("SomeUndefinedClass");
?>

Autoloading के लिए Folders Structure करना

एक common convention एक file में एक class pair करती है, class से exactly match करते हुए named (User.php में class User है), जो एक autoloader के लिए सिर्फ class name से सही file guess करना trivial बना देता है।

उदाहरण: Structuring Folders for Autoloading

php
<?php
spl_autoload_register(function ($class) {
    // Declare `$file`, set to `$class . '.php'`
    $file = $class . '.php';
    // Print "Looking for file: $file\n" to the output
    echo "Looking for file: $file\n";
});
// Call `class_exists("User")`
class_exists("User");
?>

Namespaces के साथ Autoloading

जब classes namespaces के अंदर रहती हैं, autoloader को fully-qualified name मिलता है (जैसे App\Models\User); backslashes को slashes में convert करना आपको require करने के लिए एक relative file path देता है।

उदाहरण: Autoloading with Namespaces

php
<?php
spl_autoload_register(function ($class) {
    // Declare `$path`, set to `str_replace('\\', '/', $class) . '.php'`
    $path = str_replace('\\', '/', $class) . '.php';
    // Print "Resolved path: $path\n" to the output
    echo "Resolved path: $path\n";
});
// Call `class_exists("App\\Models\\User")`
class_exists("App\\Models\\User");
?>

PSR-4 Autoloading Standard

PSR-4 उस namespace-to-folder mapping को एक standard की तरह formalize करता है जिस पर हर PHP package भरोसा कर सकता है, यही वजह है कि modern projects अब शायद ही कभी हाथ से एक custom autoloader लिखते हैं -- Composer इसे PSR-4 configuration के through handle करता है।

उदाहरण: PSR-4 Autoloading Standard

php
<?php
// composer.json: "autoload": { "psr-4": { "App\\": "src/" } }
echo "Composer maps the App\\ namespace to the src/ folder automatically";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. class name और namespace से match न करने वाले file names या paths इस्तेमाल करना, ताकि autoloader class ढूँढ न सके।
  2. किसी class इस्तेमाल करने से पहले spl_autoload_register से autoloader register करना भूल जाना।
  3. case-sensitive file systems पर case गलत करना, जैसे class User के लिए user.php।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.