{"id":15496277,"url":"https://github.com/imranhsayed/autoloaders-with-composer","last_synced_at":"2025-04-22T21:28:45.894Z","repository":{"id":97032248,"uuid":"223132142","full_name":"imranhsayed/autoloaders-with-composer","owner":"imranhsayed","description":"⌛ Demo for autoloading with Composer","archived":false,"fork":false,"pushed_at":"2019-11-21T10:47:24.000Z","size":14,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-28T09:04:07.800Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imranhsayed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-11-21T09:04:04.000Z","updated_at":"2021-11-10T13:37:40.000Z","dependencies_parsed_at":"2024-03-09T02:45:17.126Z","dependency_job_id":null,"html_url":"https://github.com/imranhsayed/autoloaders-with-composer","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/imranhsayed%2Fautoloaders-with-composer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranhsayed%2Fautoloaders-with-composer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranhsayed%2Fautoloaders-with-composer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranhsayed%2Fautoloaders-with-composer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imranhsayed","download_url":"https://codeload.github.com/imranhsayed/autoloaders-with-composer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250327529,"owners_count":21412449,"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":[],"created_at":"2024-10-02T08:24:11.883Z","updated_at":"2025-04-22T21:28:45.889Z","avatar_url":"https://github.com/imranhsayed.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Autoloading with Composer\n\n✅ Demo for autoloading with composer.\n\n[Useful blog on this](https://medium.com/tech-tajawal/php-composer-the-autoloader-d676a2f103aa)\n\n## Steps taken\n\n✅ 1. Create two classes `A.php` and `B.php` in classes directory.\n\n✅ 2. Create a `composer.json` file in the root of your project and add the below code for autoloading.\n\n```ruby\n    {\n      \"autoload\": {\n        \"classmap\": [\n          \"classes/\"\n        ]\n      }\n    }\n```\n\n\u003e Using `classmap`, above we are telling composer that this is the way to do mapping, and it is a very basic way to map namespaces to paths.\n\n✅ 3. Now when we run the command `composer install` in the root of our project, it will replace our spl_autoload_register function in the `autoload.php` with its own code and also\ncreate the below files automatically in the composer directory.  \n\n```ruby\n└── vendor\n    ├── autoload.php\n    └── composer\n        ├── ClassLoader.php\n        ├── LICENSE\n        ├── autoload_classmap.php\n        ├── autoload_namespaces.php\n        ├── autoload_psr4.php\n        ├── autoload_real.php\n        ├── autoload_static.php\n        └── installed.json\n```\n\n✅ 4. Require this `autoload.php` file in `index.php` and instantiate the `class A`.\n\n```ruby\nrequire __DIR__ . '/vendor/autoload.php';\n/**\n * Load A class\n * By using spl autoload register,\n * PHP is telling you “I will give you the chance to go and load your class even if you didn’t load it before your statement $a = new A();\n * after that, I will throw an error if it didn’t work”.\n */\n$a = new A();\n```\n\n\n        \n✅ 5.It will now load all of this files automatically and the class that is instantiated . So if we run in index.php, we get\n\n```ruby\n// check the list of all loaded files\necho '\u003cpre\u003e';\nprint_r( get_included_files() );\n```\n\n```ruby\nArray\n(\n    [0] =\u003e /Applications/MAMP/htdocs/autoloaders-with-composer/index.php\n    [1] =\u003e /Applications/MAMP/htdocs/autoloaders-with-composer/vendor/autoload.php\n    [2] =\u003e /Applications/MAMP/htdocs/autoloaders-with-composer/vendor/composer/autoload_real.php\n    [3] =\u003e /Applications/MAMP/htdocs/autoloaders-with-composer/vendor/composer/ClassLoader.php\n    [4] =\u003e /Applications/MAMP/htdocs/autoloaders-with-composer/vendor/composer/autoload_static.php\n    [5] =\u003e /Applications/MAMP/htdocs/autoloaders-with-composer/classes/A.php\n)\n``` \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimranhsayed%2Fautoloaders-with-composer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimranhsayed%2Fautoloaders-with-composer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimranhsayed%2Fautoloaders-with-composer/lists"}