PHP Autoloading
In this page:
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
// Without autoloading: require 'User.php'; require 'Product.php'; ...
echo "Autoloading loads a class file automatically the first time it's used";
?>
Login to try C/C++/Java/PHP code in the editor
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
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");
?>
Login to try C/C++/Java/PHP code in the editor
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
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");
?>
Login to try C/C++/Java/PHP code in the editor
Namespaces के साथ Autoloading
जब classes namespaces के अंदर रहती हैं, autoloader को fully-qualified name मिलता है (जैसे App\Models\User); backslashes को slashes में convert करना आपको require करने के लिए एक relative file path देता है।
उदाहरण: Autoloading with Namespaces
<?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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// composer.json: "autoload": { "psr-4": { "App\\": "src/" } }
echo "Composer maps the App\\ namespace to the src/ folder automatically";
?>
Login to try C/C++/Java/PHP code in the editor
- class name और namespace से match न करने वाले file names या paths इस्तेमाल करना, ताकि autoloader class ढूँढ न सके।
- किसी class इस्तेमाल करने से पहले
spl_autoload_registerसे autoloader register करना भूल जाना। - case-sensitive file systems पर case गलत करना, जैसे class
Userके लिएuser.php।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser