Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PSR-4 autoloading work in composer for custom libraries?

I use the following directory structure based on my understanding of how namespaces in PHP work:

project_root
    app/
    |    lib/
    |    |    MyCompany/
    |    |    |    Utility/
    |    |    |    |    Logger.php
    |    |    |    Core/
    |    |    |    |    User.php
vendor/
    composer/
    symfony/
    guzzle/
bootstrap.php
composer.json

According to the PSR-4 specification, a fully qualified class name has the following form:

\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>

Question 1:

From my directory structure above, is the assumption below correct?

  • NamespaceName = MyCompany
  • SubNamespaceNames = Utility | Core
  • ClassName = Logger | User

Question 2:

If my bootstrap.php file contains the following:

<?php
require 'vendor/autoload.php';

How would I configure the 'autoload' section of composer.json to autoload the classes in the MyCompany directory? Such that I would be able to create an instance of Logger in bootstrap.php

like image 406
Scot Calvin Avatar asked Jan 06 '15 15:01

Scot Calvin


People also ask

What's the purpose of PSR 4 autoloading?

Overview. This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

What is autoloading in Composer?

Autoloading: The classmap Directive You just need to provide a list of directories, and Composer will scan all the files in those directories. For each file, Composer will make a list of classes that are contained in that file, and whenever one of those classes is needed, Composer will autoload the corresponding file.

What is autoloading classes in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

How does autoload PHP work?

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.


1 Answers

Taken from the documentation you linked:

{
    "autoload": {
        "psr-4": {
            "MyCompany\\": "app/lib/MyCompany/",
        }
    }
}

This is pretty self explanatory, it simply tells the autoloader that app/lib/MyCompany is the root for the MyCompany\ namespace.

You would then be able to use the class as \MyCompany\Utility\Logger.

Note that in PSR-4, unlike PSR-0, you'd normally omit MyCompany from the directory structure, and just use app/lib/.

like image 75
Jeremiah Winsley Avatar answered Oct 18 '22 10:10

Jeremiah Winsley