Skip to content

lixoten/php-namespaces-and-composer-autoload-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

example of composer autoload and namespaces

index2.php - Using "require_once" with no autoloading

  • namespaces are not required for each file/class, but we can use them... optional.
require_once "./app/Email.php";

index.php - composer autoload

  • namespaces are required, it is used to map directories in composer.json
  • we do not need require_once for each class
  • we do need require_once for composer autoload
require_once "./vendor/autoload.php";
  • Some notes for the composer.json file
    • current folder
      • "namespace1\\": "./folder",
    • Back up 1 dir, OUTSIDE the project, or outside the public_html folder
      • "namespace2\\": "../folder",
    • root, usually files in public_html
      • "": ".",
/* 
"autoload": {
        "psr-4": {
            "MYAPP\\": "./app/",
            "MYCORE\\": "./core/",
            "": "."
        }
    },
*/

if using git

  • Make to check in/add both composer.json and `composer.lock' to repo too.
  • Add vendor to .gitignore, not need in git repo.

Notes

  • The Environment I tested this as localhost I created in Windows 11. Local apache server and php

About

A simple example that uses namespaces and autoloading with psr 4 and composer

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages