php composer: Let's make effective use of autoload !

Other language site
ja ja
Google Translate
  • -

    シェア
  • ---

    LINEで送る
  • -

    はてなブックマーク
    ブックマーク
  • -

    pocket
  • -

    rss
php logo

php composer has "autoload".

"autoload" is a function to automatically load classes by using "use" without having to import package class files with "require".

"autoload" aggregates the class information of the package and saves it as a configuration file (.php) when executing package install / update with "composer".

Structure of "autoload"

You do not need to "require" all the .php files in the package

"autoload" creates / updates the "vendor/autoload.php" file when installing / updating packages with commands such as "composer install", "composer update", "composer require"

By simply writing one line of "require vendor/autoload.php", it becomes possible to use the classes of all the packages installed under vendor with "use".

In addition to classes, interfaces, functions, and constants can also be used with "use".

<?php
use My\Full\ClassName;
use My\Full\InterfaceName;
 
// >= PHP 5.6
use function My\Full\functionName as func;
 
// >= PHP 5.6
use const My\Full\CONSTANT;

It is very convenient because you do not have to "require" all the .php files in the package.

In the Laravel framework, you do not need to do anything because you are "require" in "public/index.php" created during installation.

Location of "autoload" file

"autoload" is itself a package of "composer", and automatically installs "composer" package after installing any package.

That resource is created in "vendor/autoload.php", "vendor/composer/***".
There are several setting methods for "autoload".

  • Save the array that associates class and .php file in .php file.
  • Save the array that associates namespace and .php file in .php file

Etc.

The files to be created are "autoload_classmap.php", "autoload_files.php" etc in "vendor/composer/" directory.

The file that aggregates the array data created in "vendor/composer/" is "vendor/autoload.php".

"autoload" will be processed in the following order.

  • require vendor/autoload.php
  • vendor/composer/***. Execute autoload from php file

Some setting methods will be explained later. Let's start with the setting method of "autoload".

How to set "autoload"

The "autoload" setting is described in composer.json file. If the composer.json file does not exist in the package, you need to create it.

Create composer.json file

Create a composer.json file in the package's home directory. In the case of new creation, execute the command in the home directory.

php ~/bin/composer init

Although you can ask for various inputs, you can edit it later, so I'm OK with all "Enter" in the meantime.

Editing composer.json file

For autoload settings,

"autoload": {

}

It is only part. Here I will show you the completed sample.

{
    "name": "my_pkg/laravel-ext",
    "description": "Extended or customized package of Laravel",
    "license": "MIT",
    "authors": [
        {
            "name": "******",
            "email": "*****@******"
        }
    ],
    "require": {
        "php": ">=7.0.0"
    },
    "autoload": {
        "psr-4": {
            "MY_PKG\\": "src/MY_PKG/"
        }
    },
    "config": {
        "sort-packages": true
    }
}

The sample is a package of the Laravel extension function program. Since Laravel conforms to psr, it sets psr-4.

Details will be explained later, but in applications that conform to psr such as Laravel, you only have to set psr-4.

syntax check of composer.json

Execute the command at the location of the composer.json file and check the syntax of composer.json.

php ~/bin/composer validate

An error will occur with composer command if syntax error remains. Always run it when editing composer.json file.

Here's how to create a package for composer.

Types specified with "autoload"

files

"autoload": {
    "files": [
        "src/functions/func1.php",
        "src/const/const1.php",
    ]
}

Autoload a .php file that defines functions, const, etc. that are not class files.

Files where results are saved

  • vendor/composer/autoload_files.php

psr-4

"autoload": {
    "psr-4": { "Vendor\\": ["src/Vendor/", "lib/Vendor/"] }
    }
}
"autoload": {
    "psr-4": { "Vendor\\": "src/vendor/" }
}

For packages used for application development according to psr like Laravel, set psr-4.
In psr-4, it is necessary to match the namespace and the directory structure. Specify the location of the top level namespace directory.

It is also possible to specify multiple items by [].

If psr-4 is specified, the namespace and the directory structure must match, otherwise "Class not Found" will occur when running the application.

Since the error of the package itself does not occur, it seems as if there is a bug in the application.

In this case, autoload linkage information is old, there is a possibility of a package bug.

When psr-4 is set, there is no need to set psr-0, classmap which will be described later. In psr-4, contents to be set with psr-0, classmap are also included.

Files where results are saved

  • vendor/composer/autoload_classmap.php
  • vendor/composer/autoload_namespaces.php
  • vendor/composer/autoload_psr-4.php
  • vendor/composer/autoload_static.php

psr-0

"autoload": {
    "psr-0": {
        "Vendor\\": "src/Vendor/",
        "Vendor\\Namespace\\": "src/Vendor/",
        "Vendor_Namespace_": "src/"
    }
}

It is used in projects conforming to psr-2 etc. We do not use almost anything other than using it for continued development etc. that has been continuing since several years ago.

Files where results are saved

  • vendor/composer/autoload_classmap.php

classmap

"autoload": {
    "classmap": ["src/", "lib/", "Something.php"]
}

Link classmap and class file. It is used when not complying with psr such as WordPress.

Files where results are saved

  • vendor/composer/autoload_classmap.php

exclude-from-classmap

"autoload": {
    "exclude-from-classmap": ["/Tests/", "/test/", "/tests/"]
}

Make exclusion setting of "autoload". Exclude unnecessary class in production environment such as test class.

Files where results are saved

  • vendor/composer/autoload_classmap.php

"autoload" of development environment

"autoload-dev": {

}

Set "autoload" of the development environment. Specify the test class and so on. Only the root directory can be specified.

Updating "autoload"

php ~/bin/composer dump-autoload

Rebuild the information on the "autoload" class map. Execute when "Class not Found" occurs.

important point

php ~/bin/composer dump-autoload -o

With the option -o, "autoload" is optimized. There is a feature that the speed of "autoload" becomes faster.

However, attention is necessary. When this option is attached, rules of psr-4 are ignored when class map is created.
Namespace and class files of directory structure not conforming to psr-4 are also registered in the class map, so it works normally.

  • Development environment has no option
  • Production environment has option

It would be better to use it.

SNS also distributes articles.
Leave a Reply

*

If you like this article, share it!