|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +# 🏗 PHP Auto Autoloader |
| 4 | + |
| 5 | +With this autoloader, you do not need any more, no matter where you have a master in your root, the autoloader of MB tnado Ai will find it. The classes found can be indexed to retry without searching. |
| 6 | + |
| 7 | +# Usage |
| 8 | + |
| 9 | +</div> |
| 10 | + |
| 11 | +Instantiate your classes, interfaces, traits or even abstract classes, no matter where. |
| 12 | +The only thing you have to do is to integrate. |
| 13 | + |
| 14 | +## Packagist with Composer |
| 15 | + |
| 16 | +This solution extends the vendor autoloader because it calls the files with certain criteria. The extension allows you to call classes wherever the Autoloader is involved. |
| 17 | + |
| 18 | +Download [Composer](https://getcomposer.org/) local or global. Check for more [Tutorial - NetBeans with Composer and Packagist](https://www.tnado.com/blog/netbeans-with-composer-and-packagist-the-php-package-manager/) for this one. |
| 19 | + |
| 20 | +You found the Autoloader package on [Packagist - PHP Auto Autoloader](https://packagist.org/packages/prod3v3loper/php-auto-autoloader). |
| 21 | + |
| 22 | +Add the Autoloader dependencie to the **composer.json** and set the autoload. |
| 23 | +```json |
| 24 | +{ |
| 25 | + ... |
| 26 | + "autoload": { |
| 27 | + "psr-4": { "Aautoloder\\": "autoload/src" } |
| 28 | + }, |
| 29 | + "require": { |
| 30 | + "prod3v3loper/php-auto-autoloader": ">=1.0" |
| 31 | + } |
| 32 | + ... |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +Now run the composer install command with php |
| 37 | +``` |
| 38 | +php composer.phar install |
| 39 | +``` |
| 40 | +e.g. |
| 41 | +``` |
| 42 | +composer install |
| 43 | +``` |
| 44 | + |
| 45 | +This is the vendor autoloader invites our autoloader and now we do not need to specify any more class and can load all our classes. |
| 46 | + |
| 47 | +`index.php` |
| 48 | +```php |
| 49 | +<?php |
| 50 | +// Define the document root project folder |
| 51 | +define('MBT_DOCUMENT_ROOT', __DIR__); |
| 52 | + |
| 53 | +// Load the autoloader |
| 54 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 55 | +new \Aautoloder\Loader(array(MBT_DOCUMENT_ROOT)); |
| 56 | +?> |
| 57 | +<!DOCTYPE html> |
| 58 | +<html> |
| 59 | + <head> |
| 60 | + <meta charset="UTF-8"> |
| 61 | + <title>Prod3v3loper - Autoload</title> |
| 62 | + </head> |
| 63 | + <body> |
| 64 | + <?php |
| 65 | + |
| 66 | + // METHOD 1: Fastest way, the namespace is foldername structure and filename is equal to filename |
| 67 | + $class_one = new testclasses\first_class(); |
| 68 | + $class_one->method_from_first_class(); |
| 69 | + |
| 70 | + // METHOD 2: The slower way, namespace is equal to folder structure but the classname is differnt to the filename |
| 71 | + $class_two = new testclasses\classes\second_class(); |
| 72 | + $class_two->method_from_second_class(); |
| 73 | + |
| 74 | + // METHOD 3: The slowest way, here ist nothing euqal |
| 75 | + $three_class = new name_space\namespace2\third_class(); |
| 76 | + $three_class->method_from_third_class(); |
| 77 | + |
| 78 | + ?> |
| 79 | + </body> |
| 80 | +</html> |
| 81 | +``` |
| 82 | + |
| 83 | +## Git clone |
| 84 | + |
| 85 | +Get per [Git](https://git-scm.com/) or [download](https://github.com/prod3v3loper/php-auto-autoloader/archive/master.zip) and use it. |
| 86 | + |
| 87 | +``` |
| 88 | +git clone https://github.com/prod3v3loper/php-auto-autoloader.git /projects/ |
| 89 | +``` |
| 90 | + |
| 91 | +So we use it without a vendor and can start a direct call. |
| 92 | + |
| 93 | +`index.php` |
| 94 | +```php |
| 95 | +<?php |
| 96 | +// Define the document root project folder |
| 97 | +define('MBT_DOCUMENT_ROOT', __DIR__); |
| 98 | + |
| 99 | +// Load the autoloader |
| 100 | +require_once './autoload/src/Loader.php'; |
| 101 | +new \Aautoloder\Loader(array(MBT_DOCUMENT_ROOT)); |
| 102 | +?> |
| 103 | +<!DOCTYPE html> |
| 104 | +<html> |
| 105 | + <head> |
| 106 | + <meta charset="UTF-8"> |
| 107 | + <title>Prod3v3loper - Autoload</title> |
| 108 | + </head> |
| 109 | + <body> |
| 110 | + <?php |
| 111 | + |
| 112 | + // METHOD 1: Fastest way, the namespace is foldername structure and filename is equal to filename |
| 113 | + $class_one = new testclasses\first_class(); |
| 114 | + $class_one->method_from_first_class(); |
| 115 | + |
| 116 | + // METHOD 2: The slower way, namespace is equal to folder structure but the classname is differnt to the filename |
| 117 | + $class_two = new testclasses\classes\second_class(); |
| 118 | + $class_two->method_from_second_class(); |
| 119 | + |
| 120 | + // METHOD 3: The slowest way, here ist nothing euqal |
| 121 | + $three_class = new name_space\namespace2\third_class(); |
| 122 | + $three_class->method_from_third_class(); |
| 123 | + |
| 124 | + ?> |
| 125 | + </body> |
| 126 | +</html> |
| 127 | +``` |
| 128 | +<div align="center"> |
| 129 | + |
| 130 | +# Core Settings & Debug |
| 131 | + |
| 132 | +</div> |
| 133 | + |
| 134 | +`autoload/core.config.php` |
| 135 | + |
| 136 | +Indexing of the found classes, default is `true`. |
| 137 | +```php |
| 138 | +define('MBT_CORE_AUTOLOAD_INDEX', true); |
| 139 | +``` |
| 140 | + |
| 141 | +Maximum number of lines to read in a file, default is `49`. |
| 142 | +```php |
| 143 | +define('MBT_CORE_AUTOLOAD_READ_MAX_LINES', 49); |
| 144 | +``` |
| 145 | + |
| 146 | +Set this to false so that you no longer see the debugging, default is `true`. |
| 147 | +```php |
| 148 | +define('MBT_DEBUG_DISPLAY_AUTOLOAD', true); |
| 149 | +``` |
| 150 | + |
| 151 | +Look at which classes where were found, default is `true`. |
| 152 | +```php |
| 153 | +define('MBT_DEBUG_DISPLAY_AUTOLOAD_SEARCH', true); |
| 154 | +``` |
| 155 | + |
| 156 | +<div align="center"> |
| 157 | + |
| 158 | +# Root |
| 159 | + |
| 160 | +</div> |
| 161 | + |
| 162 | +The complete path is the directory path, that the autoloader get by self. |
| 163 | + |
| 164 | +DEFAULT: `MBT_DOCUMENT_ROOT` |
| 165 | + |
| 166 | +The autoloader define get the web root by self after set `MBT_DOCUMENT_ROOT` |
| 167 | +```php |
| 168 | +define('MBT_DOCUMENT_ROOT', __DIR__); |
| 169 | + |
| 170 | +define('MBT_SERVER_ROOT', str_replace(MBT_DOCUMENT_ROOT, '', str_replace(filter_input(INPUT_SERVER, 'DOCUMENT_ROOT'), '', str_replace("\\", "/", MBT_DOCUMENT_ROOT)))); |
| 171 | +define('MBT_HTTP_ROOT', get_protocol() . get_host() . MBT_SERVER_ROOT); |
| 172 | +``` |
| 173 | + |
| 174 | +<div align="center"> |
| 175 | + |
| 176 | +# Loader methods |
| 177 | + |
| 178 | +</div> |
| 179 | + |
| 180 | +The autoloader finds everything yourself you do not have to do anything except write your class and instance and use. |
| 181 | + |
| 182 | +Method | Namespace (with class) | Path | Load Time |
| 183 | +------------ | ------------ | ------------- | ------------- |
| 184 | +1 | testclasses\first_class | php-auto-autoloader/testclasses/first_class.php | 0.0 sec. |
| 185 | +2 | testclasses\classes\second_class | php-auto-autoloader/testclasses/classes/second.php | 0.002 sec. |
| 186 | +3 | name_space\namespace2\third_class | php-auto-autoloader/testclasses/classThree.php | 0.011 sec. |
| 187 | + |
| 188 | +## METHOD 1: |
| 189 | + |
| 190 | +> This is the fastest way |
| 191 | +
|
| 192 | +That means we take the website root path and namespace as a folder path and the classname we put together with these. |
| 193 | + |
| 194 | +### Example |
| 195 | + |
| 196 | +**The instance:** |
| 197 | +`new testclasses\first_class();` |
| 198 | + |
| 199 | +Type | Sample | Description |
| 200 | +---- | ------ | ------ |
| 201 | +PATH | `/project/sites/mywebsite/` | MBT_DOCUMENT_ROOT |
| 202 | +NAMESPACE | `testclasses` | Like the folder name |
| 203 | +CLASS | `first_class` | Like the file name without extension |
| 204 | +Then the result example | `/project/sites/mywebsite/testclasses/first_class.php` | |
| 205 | + |
| 206 | +## METHOD 2: |
| 207 | + |
| 208 | +> This method is slightly slower than the first, so 0.03 - 0.05 seconds |
| 209 | +
|
| 210 | +This function namespace as folder path and force only this path for class `second_class` (Example) file. |
| 211 | +However, this only occurs when the file does not match the specified class name. |
| 212 | +This means every file found in this folder is opened and searched for the classname. |
| 213 | +As soon as the used class exists in a file, this is integrated and can use it. |
| 214 | + |
| 215 | +### Example |
| 216 | + |
| 217 | +**The instance:** |
| 218 | +`new testclasses\classes\second_class();` |
| 219 | + |
| 220 | +Type | Sample | Description |
| 221 | +---- | ------ | ------ |
| 222 | +PATH | `/project/sites/website/` | MBT_DOCUMENT_ROOT |
| 223 | +NAMESPACE | `testclasses\classes` | Like the folder name |
| 224 | +Then the result example | `/project/sites/mywebsite/testclasses/classes/second_class.php` | |
| 225 | + |
| 226 | +## METHOD 3: |
| 227 | + |
| 228 | +> This method is the slowest, but found class anything where |
| 229 | +
|
| 230 | +This method is the slowest, because it scans all your folders. |
| 231 | +No matter how much files you have, all are opened, read and searched for the classname. |
| 232 | +This method intervenes only when none of the other methods was successful. So if the namespace was not just a folder name and the class name was not filename. |
| 233 | + |
| 234 | +### Example |
| 235 | + |
| 236 | +**The instance:** |
| 237 | +`new testclasses\classes\third_class();` |
| 238 | + |
| 239 | +Type | Sample | Description |
| 240 | +---- | ------ | ------ |
| 241 | +PATH | `/project/sites/mywebsite/` | MBT_DOCUMENT_ROOT |
| 242 | +Then the result example | `/project/sites/mywebsite/testclasses/classThree.php` | |
| 243 | + |
| 244 | +<div align="center"> |
| 245 | + |
| 246 | +# Authors |
| 247 | + |
| 248 | +**prod3v3loper** - *All works* |
| 249 | + |
| 250 | +# License |
| 251 | + |
| 252 | +MIT - prod3v3loper |
| 253 | + |
| 254 | +</div> |
0 commit comments