Try using Composer
environment
First book: 2020/09/03 PC: macOS 10.15.6 Composer: v1.10.10 php 7.4.9
What is #Composer?
Composer is an application-level package management system that provides a standard format for managing software and required library dependencies for the PHP programming language.
In short, it seems to handle library dependencies together. On the contrary, if you don’t use the library, you probably don’t need it. (Well, if there is a possibility to use it later, it may be prepared first)
#Install Use homebrew.
% brew install composer
only this. By the way, you don’t have to worry about local or global now (should)
For the time being, check if the version information comes out with % composer -v
.
#Add to development environment
Create a composer.json
file directly under the folder to be created, and for the time being
{}
Describe as. (If you have decided which library to use, you may want to write it at this point.)
Then run the following from the terminal
% composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
You should now have a vender folder.
#Exclude from git (optional)
If you are using git with vscode, basically all files / folders will be the target of git, which is annoying, so I will exclude it.
First, create a .gitignore
file in the same hierarchy as composer.json
Describe the file name to be excluded from git in this file.
This time I want to exclude the vendor folder, so
vendor
.gitignore
(Is it okay to exclude .gitignore from git?)
Complete
Now you are ready to go. The rest is a php file
require __DIR__.'/vendor/autoload.php';
You can use the library just by loading.
#. Well, it’s okay to write so far, but if you don’t use the library that you’ve seen so far, you don’t need it at all. Here comes another role of composer. It is a system that reads only what you need, when you need it.
There is a function called autoload in php, and if the called class is not loaded, it will be loaded. There is a function called. 1
Since composer handles this all at once, it seems that it can handle library dependencies and so on.
So, take the class you created into composer and set it so that it can be read at an appropriate time.
#Preparing classes
If there is no class to explain for the time being, the story will not proceed, so prepare an appropriate class.
<? php
namespace tes;
class ShowText
{
public function EchoText (string $ str): void
{
echo htmlspecialchars ($ str);
}
}
A class that has a function to escape and output. Create a php folder and prepare this.
..
├── composer.json
├── vendor
│ ├── autoload.php
│ └── (Other folders)
└── php
└── ShowText.php
It’s like this now.
Add to # composer
Add the following to composer.json. (If the inside of json is empty, copy it below. If you are writing, it is the same position as require etc.)
{
"autoload": {
"psr-4": {
"tes \\": "php"
}
}
}
By the way, in order to use the description method called psr-4, the key must be namespace (+ \\), the value must be the folder structure up to the corresponding file, and the class name and file name must be the same. It seems.
When you finish writing
% composer dump-autoload
To make the autoloader recognize it.
Try to run
<? php
declare (strict_types = 1);
require_once __DIR__. "/vendor/autoload.php";
$ a = new tes \ ShowText ();
$ a-> EchoText ("test display");
Test display
You can also use use if you feel the class declaration is long.
<? php
declare (strict_types = 1);
use tes \ ShowText;
require_once __DIR__. "/vendor/autoload.php";
$ a = new ShowText ();
$ a-> EchoText ("test display");
In this way, even if you write use before autoload, the ShowText file is read in the new ShowText part, so there seems to be no problem even if you use use.
At the end
It’s a big deal, so let’s find some libraries. ..
#Reference site
How to use Composer
Remove .vscode directory from git management with Visual studio code
Automatically load your own class with \ autoload \
in \ composer \
-
Class autoloading,spl_autoload_register/ja/function.spl-autoload-register.php), ↩︎