{"id":18772481,"url":"https://github.com/eftec/autoloadone-example","last_synced_at":"2025-12-12T21:30:15.784Z","repository":{"id":71040952,"uuid":"292110221","full_name":"EFTEC/autoloadone-example","owner":"EFTEC","description":"Example code of AutoLoadOne Library.","archived":false,"fork":false,"pushed_at":"2020-09-01T23:39:29.000Z","size":775,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-29T08:13:45.772Z","etag":null,"topics":["example","tutorial"],"latest_commit_sha":null,"homepage":"https://www.southprojects.com/Programming/high-performance-loader-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EFTEC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-01T21:21:27.000Z","updated_at":"2020-09-02T11:34:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"fba6630e-383c-48ed-95ad-2654c56207c9","html_url":"https://github.com/EFTEC/autoloadone-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EFTEC%2Fautoloadone-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EFTEC%2Fautoloadone-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EFTEC%2Fautoloadone-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EFTEC%2Fautoloadone-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EFTEC","download_url":"https://codeload.github.com/EFTEC/autoloadone-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239680825,"owners_count":19679505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["example","tutorial"],"created_at":"2024-11-07T19:29:15.601Z","updated_at":"2025-12-12T21:30:15.747Z","avatar_url":"https://github.com/EFTEC.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# autoloadone-example\nExample code of **AutoLoadOne** Library for PHP 5.6 and higher.\n\nThe library\n\n[https://github.com/EFTEC/autoloadone](https://github.com/EFTEC/autoloadone)\n\nWhy we need this library?\n\n* It is a high performance library. It is optimized for speed and memory usage.\n* It doesn't requires a special configuration. It works with practically every project.\n* It doesn't requires a standard, PSR-0 PSR-4 or any other. And yet, it is compatible with it.\n\n![ui1](doc/flash.jpg)\n\n## What is an autoloader?\n\nLet's say the next example\n\n```php\n$obj=new \\example\\folder\\SomeClass();\n```\n\nTo runs this class, we need to include it using our code.\n\n```php\ninclude __DIR__'/folder/SomeClass.php';\n$obj=new \\example\\folder\\SomeClass();\n```\n\nBut what if the class **SomeClass** calls another class or we call more classes?.  Then we must include it manually and so on. If the include is called more than once, then the system could fail. We could avoid it using include_once but it doesn't solve all troubles.\n\nWhen we use an autoloader, it loads the file only when it is required.\n\n```php\ninclude 'autoload.php';\n$obj=new \\example\\folder\\SomeClass();\n```\n\nComposer's Autoload uses the next strategy. It relies on composer.json to configure and creates the namespace. But it requires that our code follows a strict guideline (or two), PSR-0 or PSR-4.\n\n```php\ninclude 'vendor/autoloader.php';\n$obj=new \\example\\folder\\SomeClass();\n\n```\n\nAnd **composer.json**\n\n```json\n\"autoload\": {\n  \"psr-4\": {\n    \"example\\\\folder\\\": \"example/folder/\",\n```\n\nHowever, this autoloader does the next operations:\n\n* It includes a library. It also includes another library depending in the type of autoloader.\n* It reads an array that it is loaded when **composer.json** was executed. This array has some wacky optimizations but it does some concatenations each time it is called. When is it called? Once per request of the customer!.\n\n```php\nreturn array(\n    '7b11c4dc42b3b3023073cb14e519683c' =\u003e $vendorDir . '/ralouphie/getallheaders/src/getallheaders.php',\n```\n\nThis concatenate with **$vendorDir** is executed per request x class requested. In a plain Laravel project, it is called +600 times.\n\n* It has different strategies to call the file.\n\nSince it is called per request, then it could hurt the performance of the system.  For example, if 1 customer call the website and he does 9 ajax calls, then he is doing 10 requests.\n\n**AutoLoadOne** works differently. \n\n* First, it doesn't use any **composer.json**.\n* It requires the information is compiled at least once and it stores the result in 4 variables, one for namespace =\u003e path relation, and other for namespace =\u003e filename relation.\n* It consist of only 3 methods, so PHP don't need to load different php files and classes.\n\nThe information in **AutoLoadOne** is store simply, example:\n\n```php\n'example\\folder' =\u003e '/folder',\n```\n\nSo, it is not encoded, neither it calculates the relative folders each time. But it also has an extra optimization. It could be edited manually.\n\nIf a folder is used more than once, then it could be resumed (to save memory) as\n\n```php\n$array=[]'example\\folder2\\Class1' =\u003e '|1|',\n'example\\folder2\\Class2' =\u003e '|1|'...];\n\n// And in other array\n\n$array=[1 =\u003e '/folder2/MultipleClasses.php'];\n```\n\nIf we want to call the class example\\folder2\\Class1, then the system reads the second array and creates the full path with it. This conversion is only executed when the class is requested, so it doesn't harm the performance if the class is not called.  \n\n\n\n## PSR-0\n\nMandatory\n\n1. A fully-qualified namespace and class must have the following structure \\\u003cVendor Name\u003e\\(\u003cNamespace\u003e\\)*\u003cClass Name\u003e\n2. Each namespace must have a top-level namespace (“Vendor Name”).\n3. Each namespace can have as many sub-namespaces as it wishes.\n4. Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.\n5. Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.\n6. The fully-qualified namespace and class are suffixed with .php when loading from the file system.\n7. Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case.\n\n## PSR-4\n\n1. The term “class” refers to classes, interfaces, traits, and other similar structures.\n\n2. A fully qualified class name has the following form:\n\n   ```\n    \\\u003cNamespaceName\u003e(\\\u003cSubNamespaceNames\u003e)*\\\u003cClassName\u003e\n   ```\n\n   1. The fully qualified class name MUST have a top-level namespace name, also known as a “vendor namespace”.\n   2. The fully qualified class name MAY have one or more sub-namespace names.\n   3. The fully qualified class name MUST have a terminating class name.\n   4. Underscores have no special meaning in any portion of the fully qualified class name.\n   5. Alphabetic characters in the fully qualified class name MAY be any combination of lower case and upper case.\n   6. All class names MUST be referenced in a case-sensitive fashion.\n\n3. When loading a file that corresponds to a fully qualified class name …\n\n   1. A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a “namespace prefix”) corresponds to at least one “base directory”.\n   2. The contiguous sub-namespace names after the “namespace prefix” correspond to a subdirectory within a “base directory”, in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.\n   3. The terminating class name corresponds to a file name ending in `.php`. The file name MUST match the case of the terminating class name.\n\n4. Autoloader implementations MUST NOT throw exceptions, MUST NOT raise errors of any level, and SHOULD NOT return a value.\n\n## AutoLoadOne\n\nIt works with any case, PSR-0, PSR-4, no namespaces, different folders, and even no classes.  If you are using PSR-4, it works  out of the box.\n\n\n\n## How to start it.\n\n### 1) Download the library or include via composer.\n\nYou can download the file AutoLoadOne.php\n\n[https://github.com/EFTEC/autoloadone](https://github.com/EFTEC/autoloadone)\n\nOr you can install via composer\n\n\u003e composer require eftec/autoloadone\n\nThis library doesn't have any dependency, so it could also  be downloaded and copied in any folder.\n\n### 2) create the next file in the root of your project\n\n```php\n\u003c?php\n\n// if true then it also include files with autoloader\ndefine('_AUTOLOAD_COMPOSERJSON',true);\n// we include our library manually\ninclude 'vendor/eftec/autoloadone/AutoLoadOne.php';\n```\n\n### 3) And runs it (as a webpage)\n\n![ui1](doc/ui1.jpg)\n\nThis code scans the folder and add the classes, interface, structs and simple PHP files and collects if there is information to store. It could take a while (depending on the size of the project). You will need to run it every time you add a new PHP file, unless you are a new class in the same folder and using the same namespace than other classes (it is what we do it anyways)\n\n### 4) Consuming our class. \n\nIt will generate the file called **autoload.php** and it is our autoload.\n\nWe could call it as follow.\n\n```php\n\u003c?php\ninclude 'autoload.php';\n```\n\nAnd that's it.\n\n## How to usage?\n\nFirst, this generator or autoloader doesn't requires a specific structure folder or even edit the composer.json. This library ignores composer.json.\n\n### A simple folder and a class\n\n📁 folder/SomeClass.php\n\n```php\nnamespace example\\folder;\n\nclass SomeClass\n{\n}\n```\n\nLet's say the next class, where the name of the class is equals than the name (**SomeClass**) of the file (**SomeClass.php**).   How we could add this class to the autoloader? If the class is inside the root folder, then it is already included.  \n\nThe library generates the next line inside autoload.php\n\n```php\n$arrautoload = [\n\t'example\\folder' =\u003e '/folder'..]\n```\n\nIt says that if we want to look at some class with the namespace example\\folder, then we will look at the folder called \"folder\".\n\n### A simple PHP file with multiple classes.\n\nWhy we need a file with multiple classes? Sometimes we need files that are strictly coupled because the business logic of the project or because we want to reduce the number of files of the project.\n\n📁 folder2/MultipleClasses.php\n\n```php\nnamespace example\\folder2;\n\nclass Class1 {\n    \n}\n\nclass Class2 {\n    \n}\n```\n\n**AutoLoadOne** stores all classes inside this file as follow\n\n```php\n$arrautoloadCustom = [\n   'example\\folder2\\Class1' =\u003e '/folder2/MultipleClasses.php',\n   'example\\folder2\\Class2' =\u003e '/folder2/MultipleClasses.php'...\n```\n\nSince it is a simple file with two folders, then includes to the **autoload.php** differently than a simple class. It says, if we call the class **\"example\\folder2\\Class1\"**, then we call the file **'/folder2/MultipleClasses.php'** and so on.\n\n### Multiple PHP files each one with one class and sharing the same folder and namespace\n\n📁 folder3/Customer.php\n\n```php\nnamespace example\\erp;\nclass Customer\n{\n}\n```\n\n📁 folder3/Employees.php\n\n```php\nnamespace example\\erp;\nclass Employees\n{\n}\n```\n\n**AutoLoadOne** associates the namespace with the folder, so it is an efficient way to work. \n\n```php\n$arrautoload = [\n   'example\\erp' =\u003e '/folder3',\n```\n\n\u003e We don't need to run **AutoLoadOne** again if we add a new class in the same folder and in the same namespace.\n\n### Library outside of our root folder.\n\nIn our interface visual (web), we could set many configuration, including to use a library in any other folder (relative or absolute path)\n\n![ui2](doc/ui2.jpg)\n\nIn this example, we will use a relative path.\n\n📁 root folder \u003c-- our root folder\n\n📁 external project/somefolder/ClassExternal.php  \u003c-- the class that we want to include.\n\n**AutoLoadOne** adds a follow\n\n```php\n$arrautoload = [\n   'external\\somenamespace' =\u003e '../externalproject/somefolder'\n```\n\n### Adding a non-class PHP file.\n\nIt is also possible to add and execute a PHP file without classes. For this, we need to add a comment in any part of the file.\n\n```php\n/** @autorun */\n```\n\n**AutoLoadOne** adds a follow \n\n```php\n@include __DIR__.'/noclasses/noclass.php';\n```\n\nIt does a directly include to the file.\n\n### Adding a non-class PHP file with priority.\n\nThe system also allows to include a file with priority. To do this, we need to add this comment in any part of our file\n\n```php\n/** @autorun first */\n```\n\nIt is called before the regular autorun.\n\n### Excluding namespaces and folders.\n\nThis library also allows to exclude files using folders and/or namespaces.\n\nFor example, this library founds automatically the classes of composer (that we don't want to load). We could save some ram by removing it. We could remove via folder or namespace\n\n```php\n'ComposerAutoloaderInit18396353ba4e6f59a571a2e69914dd47' =\u003e '|2|real.php',\n'Composer\\Autoload\\ComposerStaticInit18396353ba4e6f59a571a2e69914dd47' =\u003e '|2|static.php'\n```\n\n![ui3](doc/ui3.jpg)\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feftec%2Fautoloadone-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feftec%2Fautoloadone-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feftec%2Fautoloadone-example/lists"}