Table of Contents
The role of composer.lock file, the role of composer.lock
Home Backend Development PHP Tutorial The role of composer.lock file, the role of composer.lock_PHP tutorial

The role of composer.lock file, the role of composer.lock_PHP tutorial

Jul 12, 2016 am 08:59 AM

The role of composer.lock file, the role of composer.lock

Basic use of Composer

Use composer.json in your project

To use composer in your project, you need to have a composer.json file. This file is mainly used to declare the relationships between packages and other element tags.


require keyword

The first thing to do in composer.json is to use the require keyword. You will tell composer which packages your project needs

Copy code The code is as follows:
{
"require": {
"monolog/monolog": "1.0.*"
}
}

As you can see, the require object will map the package name (monolog/monolog) and the package version is 1.0.*


Package naming

Basically, the name of the package is the main name/project name (monolog/monolog). The main name must be unique, but the name of the project, which is our package, can have the same name, for example: igorw/json, and seldaek/json

Package version

The version of monolog we need to use is 1.0.*, which means that as long as the version is the 1.0 branch, such as 1.0.0, 1.0.2 or 1.0.99

Two ways of version definition:

1. Standard version: Define a guaranteed version package file, such as: 1.0.2
2. A certain range of versions: Use comparison symbols to define the range of valid versions. Valid symbols include >, >=, <,<=, !=
3. Wildcard: special matching symbol *, for example, 1.0.* is equivalent to >=1.0, 4. The next important version: The best explanation of the ~ symbol is that ~1.2 is equivalent to >1.2,<2.0, but ~1.2.3 is equivalent to >=1.2.3,<1.3 version.

Installation package

Run in the project file path
Copy code The code is as follows:
$ composer install

In this way, it will automatically download the monolog/monolog file to your vendor directory.

The next thing I need to explain is

composer.lock - lock file

After installing all required packages, composer will generate a standard package version file in the composer.lock file. This will lock versions of all packages.

Use composer.lock (with composer.json of course) to control the version of your project

This is very important. When we use the install command to process it, it will first determine whether the composer.lock file exists. If it exists, it will download the corresponding version (it will not depend on the configuration in composer.json) , meaning anyone who downloads the project will get the same version.

If composer.lock does not exist, composer will read the required package and relative version through composer.json, and then create the composer.lock file

In this way, after your package has a new version, you will not be automatically updated. To upgrade to the new version, just use the update command. In this way, you can get the latest version of the package and also update you. composer.lock file.

$ php composer.phar update
or
$ composer update

Packagist (this should be composer, it feels a bit like a python package, although not so powerful, haha, with this standard, it will definitely be easy for everyone to develop websites in the future, and you can learn from many people's codes, and it will be more Convenient! )
Packagist is the main warehouse of composer. You can check it out. The basis of the composer warehouse is the source code of the package. You can obtain it at will. The purpose of Packagist is to build a warehouse that anyone can use. This means that in your file Any require package is included.

About automatic loading

In order to conveniently load package files, Composer automatically generates a file vendor/autoload.php, which you can conveniently use wherever you need to use it
require 'vendor/autoload.php';

This means that you can use third-party code very conveniently. If your project needs to use monlog, you can use it directly, they have been automatically loaded!

Copy code The code is as follows:
$log = new MonologLogger('name');
$log->pushHandler(new MonologHandlerStreamHandler('app.log', MonologLogger::WARNING));
$log->addWarning('Foo');

Of course you can also load your own code in composer.json:

Copy code The code is as follows:
{
"autoload": {
"psr-0": {"Acme": "src/"}
}
}

Composer will register psr-0 as the Acme namespace

You can define a mapping to the file directory through the namespace. The src directory is your root directory and vendor is the directory at the same level. For example, a file: src/Acme/Foo.php contains the AcmeFoo class

After you add autoload, you must reinstall to generate the vendor/autoload.php file

When we reference this file, an autoloader class will be returned, so you can put the returned value into a variable and then add more namespaces. This is very convenient if you are in a development environment , for example:
Copy code The code is as follows:
$loader = require 'vendor/autoload.php';
$loader->add('AcmeTest', __DIR__);

The role of composer.lock file

The

install command reads the composer.json file from the current directory, handles dependencies, and installs it into the vendor directory.

Copy code The code is as follows:
composer install

If the composer.lock file exists in the current directory, it will read the dependency version from this file instead of obtaining the dependency from the composer.json file. This ensures that every consumer of the library gets the same dependency version.

If there is no composer.lock file, composer will create it after handling dependencies.

In order to get the latest versions of dependencies and update the composer.lock file, you should use the update command.

Copy code The code is as follows:
composer update

This will resolve all dependencies of the project and write the exact version number to composer.lock.

If you just want to update a few packages, you can list them individually like this:

Copy code The code is as follows:
composer update vendor/package vendor/package2

You can also use wildcards for batch updates:

Copy code The code is as follows:
composer update vendor/*

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1098975.htmlTechArticleThe role of composer.lock file, the role of composer.lock The basic use of Composer Use composer.json in the project To use composer, you need to have a composer.json file, this article...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

See all articles