Tos Web Developer provides insights, tutorials, and advice around topics including content strategy, design, Drupal Development, Drupal Custom Module Development, Drupal 8 Custom themes, PHP, Server, Twig, and more


A
. For using other classes methods and properties in a class a project. One way is to first include them by "include "class file name.php" and then use it

B. Then there comes a concept of SQL autoload register this function runs when a class is not found on object creation. So when writing generic code, this function can be used to load any class of the project anywhere, 


C. Now, with PSR-4. If we are maintaining the project with the composer and envisioning third-party integration with the project We can use PSR-4 for Autoloading the classes and namespaces 


D. PSR-4, through composer.json. Inside vendor autoload file gets created and which loads another class to implement what spl_autoload_register does in a more robust way.


       i. Example: spl_autoload_register ('Autoload'); function

            my Autoloader ($class Name) { $path %3D '/path/to/class/';

            include $path.$className.'.php'; }

          //- ----------------------- $myclass = new MyClass () ;


E. How: In the composer.json file, specify the pattern for the namespace in the autoload section.


 "autoload": {  
  "psr-4": {  
   "Drupal\\Core\\": "lib/Drupal/Core",  
   "Drupal\\Component\\": "lib/Drupal/Component",  
   "Drupal\\Driver\\": "./drivers/lib/Drupal/Driver"  
  },  
 },  


F. Drupal uses the PSR-4 standard for autoloading.

No comments:

Post a Comment

| Designed And Blog Post by www.toswebdeveloper.com