2017 © Pedro Peláez
 

library autoload-test

Autoload testing between psr-0 and psr-4

image

huynh/autoload-test

Autoload testing between psr-0 and psr-4

  • Monday, May 30, 2016
  • by franknyn
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Composer Autoloading

  • Composer autoloading provides a variety of different autoloading functionality, although it is recommended to use PSR 4 autoloading which is the default implementation.
  • Autoloading classes with namespaces means it has to follow a convention, usually that convention involves using folders

Namespace/ folder convention.

  • Classes should be stored in folders according to their namespaces.
  • In general, you will create a src/ directory in your root folder, sitting at the same level as vendor/, and add your projects there. Below is an example of folder structure.
.
+-- src
    |
    +-- Book 
    |   +-- History
    |   |   +-- UnitedStates.php - namespace Book\History;
    +-- Vehicle
    |   +-- Air
    |   |   +-- Wings
    |   |   |   +-- Airplane.php - namespace Vehicle\Air\Wings;
    |   +-- Road
    |   |   +-- Car.php - namespace Vehicle\Road;
+-- tests
    +-- test.php
+-- vendor

Difference between psr-0 and psr-4 autoloading

  • See http://stackoverflow.com/questions/24868586/what-is-the-difference-between-psr-0-and-psr-4
  • See https://groups.google.com/d/msg/silverstripe-dev/i1voM50oFMk/rPouYXtnSA0J

psr-0

It is deprecated. Looking at vendor/composer/autoload_namespaces.php file you can see the namespaces and the directories that they are mapped to., (*1)

composer.json, (*2)

    "autoload": {
        "psr-0": {
            "Book\\": "src/",
            "Vehicle\\": "src/"
        }
    }    
  • Looking for Book\History\UnitedStates in src/Book/History/UnitedStates.php
  • Looking for Vehicle\Air\Wings\Airplane in src/Vehicle/Air/Wings/Airplane.php

psr-4

Looking at vendor/composer/autoload_psr4.php file you can see the namespaces and the directories that they are mapped to., (*3)

composer.json, (*4)

    "autoload": {
        "psr-4": {
            "Book\\": "src/",
            "Vehicle\\": "src/"
        }
    }    
  • Looking for Book\History\UnitedStates in src/History/UnitedStates.php
  • Looking for Vehicle\Air\Wings\Airplane in src/Air/Wings/Airplane.php

composer.json, (*5)

    "autoload": {
        "psr-4": {
            "Book\\": "src/Book/",
            "Vehicle\\": "src/Vehicle/"
        }
    }    
  • Looking for Book\History\UnitedStates src/Book/History/UnitedStates.php
  • Looking for Vehicle\Air\Wings\Airplane in src/Vehicle/Air/Wings/Airplane.php

Be ready for production

Just a reminder, before deploying your code in production, don't forget to optimize the autoloader:, (*6)

$ composer dump-autoload --optimize

This can also be used while installing packages with the --optimize-autoloader option. Without that optimization, you may notice a performance loss from 20 to 25%., (*7)

Usage

See tests/test.php, (*8)

composer install
php tests/test.php

The Versions

30/05 2016

dev-master

9999999-dev

Autoload testing between psr-0 and psr-4

  Sources   Download

The Requires