In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what composer is, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
Composer is a dependency management tool recommended by the PHP community. Composer is to PHP what npm is to Node. It is almost a necessary skill for modern PHP development. This article briefly reviews related concepts and Composer usage.
Expansion and package
The related concepts are framework and library. About the difference between framework and library, you can check out this article I wrote earlier.
Extension and package are two very similar concepts. In the PHP world, it is generally possible to understand and distinguish between the two: extension and module are equivalent, which are functional collections written in C language; packages (package) and library are equivalent, mainly functional collections realized by PHP; extensions are loaded in the form of dynamic link libraries (dll or so), while packages are loaded through require/include. Most of the time, the mixture of the two will not cause difficulties in understanding.
Common extensions include GD, ZIP, XML, MySQLi, OPCache, etc., and common packages include PHPMailer, PHPOffice, HTMLPurifier, etc.
PEAR and PECL
Before Composer became popular, PEAR and PECL were two tools (communities) better known to PHP developers. PEAR is the abbreviation of PHP extension and Application Warehouse (PHP Extension and Application Repository), the official website http://pear.php.net; PECL is the abbreviation of PHP extension Community Library (PHP Extension Community Library), and the official website http://pecl.php.net.
The difference between the two can be distinguished by extensions and packages: PECL managed extension, the source code is mostly C files, such as APC, AMPQ, etc.; PEAR managed package, features implemented by PHP, such as PHP CodeSniffer, HTTP Request, etc.; PEAR corresponding to the pear command, PECL corresponding to the pecl command, you can use these two commands to install and manage the extension and package (the build/ pickle subcommand of pear can also compile the extension in PECL). The two complement each other, and the official website uses sisters to describe their relationship.
PECL is a supplement to the official expansion, which is still active at present, and some excellent expansion has the potential to become the official expansion. Han Tianfeng god's swoole expansion is also hosted in PECL, the domestic reputation is very high. By contrast, PEAR is a thing of the past. The advent of PEAR2 and Pyrus (the next generation of PEAR package installation tools, built on PHP5.3+, the official website http://pear2.php.net) also failed to save PEAR. The decline of PEAR is accompanied by the rise of Composer, the protagonist of this article.
PEAR is positioned to "provide reusable PHP components", providing feature packs to developers in a centralized manner. The centralized release way ensures the quality of the code and brings inconvenience in maintenance: the package can only be released through the review, and the package is out of date. The packages installed by PEAR are global, you cannot install dependent packages for individual projects, and non-privileged users cannot install dependent packages themselves. Other drawbacks include poor dependency management. With the popularity of Github and the emergence of Composer, package management has entered the era of Composer. PEAR has completed its historical mission and can go with peace of mind.
Composer
Strictly speaking, Composer is positioned to rely on administrative tools rather than package managers. Composer Chinese website introduces the work of Composer as follows:
Composer will solve the problem for you like this:
A) you have a project that depends on several libraries.
B) some of these libraries depend on others.
C) you declare what you rely on.
D) Composer will find out which version of the package needs to be installed and install them (download them to your project).
Composer can do everything PEAR can do (including installing PECL extensions), and some of them can do better. Composer installs the package in the project directory by default so that ordinary users can use it normally (Composer officially recommends not to execute composer commands as root); encourages compliance with best practices (that is, the famous PSR specification, see PHP-FIG 's official website https://www.php-fig.org), which greatly promotes the standardization of PHP community coding style; Composer is a decentralized platform, and anyone can publish code packages. There is no need to review the release package, and the quality of the package is decided by the user's vote. As the successor to PEAR, Composer's performance has withstood the test of the community and has become a de facto reliance on standard management tools.
At present, Composer has formed a huge ecology, and the package of Composer far exceeds that of PEAR in quantity. Since anyone is free to release packages without review, packages in the Composer ecosystem may have hidden worries such as uneven code quality, different code styles, backdoor vulnerabilities, and so on. In addition, the dependency management of Composer is based on projects, and the same package may be installed multiple times on a machine. But generally speaking, Composer has greatly changed the development ecology of PHP and promoted code exchange and community development.
Composer usage
Composer is created for the dependence of the managed project, and the composer.json file in the project is the basis for its work. The most important part of the file is the require section, which tells Composer the package you want to install and its version, for example:
{"name": "tlanyan/foo", "version": "1.0.0",.... "require": {"php": "> = 5.4.0", "yiisoft/yii2": "> = 2.0.6", "yiisoft/yii2-swiftmailer": "*", "yiisoft/yii2-redis": "> = 2.0.0", "smarty/smarty": "
< =3.1.25", "yiisoft/yii2-smarty": ">= 2.0.0 "," phpoffice/phpexcel ":" > = 1.8.0 "," tecnickcom/tcpdf ":" ~ 6.2.0 "},....}
Then run the composer install command, and Composer automatically analyzes the dependencies and installs the most appropriate package to the vendor directory. The-v (- vv,-vvv) option prints details during the execution of the command. After installation, the autoload.php file is generated in the vendor directory. Include this file in the entry file of the project: require _ _ DIR__. "/ vendor/autoload.php"; you can then reference the interfaces and classes in the dependency package anywhere in the project.
In addition to the install command, Composer provides many other command management dependencies. Common command scenarios include: finding dependencies, introducing dependencies, installing dependencies, and updating dependencies. The corresponding commands are:
Composer search: find dependent packages based on keywords, such as finding your own published package: composer search tlanyan. This command is equivalent to package lookup on https://packagist.org; composer require: introduce dependencies and declare that a project or global (global, username global, non-system global) depends on a package, such as declaring that a swiftmailer package is required: composer require [global] "swiftmailer/swiftmailer:dev-master"; this command updates the composer.json file and installs dependencies immediately by default (--no-update option prevents default installation) The effect is equivalent to editing the composer.json file and then executing the install command; composer install: install the dependency package declared by composer.json, and the final installed version of the dependency package may depend on the presence or absence of the composer.lock file; composer update: the update depends on the latest version, which is equivalent to executing composer install after deleting the composer.lock file.
The above four commands cover most of the scenarios where Composer is used. Here are several commonly used auxiliary commands related to dependency analysis:
Composer info: view installed dependency package information, which is equivalent to composer show; composer dumpautoload: add-o option to export optimized loader; composer why (- not): see why a package is installed (not). Summary
From copying third-party code to the project (1994), to PEAR installing dependency packages (1999), to the rise of Composer (2012), the PHP community has experienced nearly 20 years of exploration. PHP, an ancient language, is constantly developing and updating, and it has been glowing and hot in the field of web. Composer, as the best tool for PHP package dependency management, deserves to be mastered by every PHP developer.
The above is all the content of this article "what is composer?" thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.