search
HomeDevelopment ToolscomposerHow to write composer package
How to write composer packageAug 09, 2019 am 09:29 AM
composer

Before introducing how to write the composer package, you can learn how to install composer and its related usage through the composer usage tutorial column.

How to write composer package

How to write the composer package?

Write your own composer package

The composer package is suitable for use when you need to cooperate with other dependent packages.

Let’s write our own composer package. First, we create an empty directory and run the following command to initialize a blank composer package

composer init

You can see the return in the command window Tip;

You need to enter the package name

This command will guide you through creating your composer.json config.`
Package name (<vendor>/<name>) :

What I wrote here is yancoo.cn/test, press Enter

You need to enter the description

Description []:

Package description content

You need to enter the author

Author [宣言 <59419979@qq.com>, n to skip]:

If there is a default value for this option, you can directly Enter

You need to enter the minimum stable version

Minimum Stability []:

This option has optional values: stable, RC, beta, alpha, dev. Generally fill in dev

Need to enter the package type

Package Type (e.g. library, project, metapackage, composer-plugin) []:

The options are already available in the prompt, we usually choose library and fill in l

Need to enter the open source agreement

License []:

Fill in according to your own situation, we fill in Apache

Set other environments or packages that the package needs to depend on

Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]?

If you need to set the dependent environment or For other packages, enter yes and press Enter, which will let you search. We set the php version for the package here to be greater than 5.6, so search for php.

Enter the version constraint to require (or leave blank to use the latest version):

Enter the minimum required version number>=5.6.0

How to write composer package

If you need to set multiple environment requirements, repeat the search Just fill it in. If you don’t need it, just press Enter without filling in the content in Search for a package:

Next, set the dependent package

Would you like to define your dev dependencies (require-dev) interactively [yes]?

us No need to press Enter directly.

{
    "name": "yancoo.cn/test",
    "description": "This is a test package,form yancoo.cn,author is siam.",
    "type": "l",
    "require": {
        "php": ">=5.6.0"
    },
    "license": "Apache",
    "authors": [
        {
            "name": "宣言",
            "email": "59419979@qq.com"
        }
    ],
    "minimum-stability": "dev"
}
Do you confirm generation [yes]?

Confirm the message, yes and press Enter. Initialization is complete

Do you confirm generation [yes]? yes
Would you like to install dependencies now [yes]? yes

The installation is complete. You can see that our directory has generated the following structure.

How to write composer package

vendor is the address where composer stores packages. All packages are stored in this path for unified management, and composer provides the function of automatic loading of namespaces. We are using We only need to introduce the unified entry file of composer

require "vendor/autoload.php";

But we also need to write in the configuration file of the package which directory needs to be automatically loaded to be effective!

Edit composer.json file

How to write composer package

The path here needs to be determined according to your own, you can also refer to the above writing method .

After writing, you need to run the command line composer dump-autoload to update the composer's namespace and folder mapping relationship.

The previous step is very important. If you miss it, it will not be executed normally.

Then we create the src folder, then create the siam folder, and create the Test.php file in it

Write the namespace Siam; According to the psr-4 specification, the class name must be the same as the file name same.

<?php
//Test.php文件
namespace Siam;
class Test
{
    function test()
    {
        echo "Form Test -> test()";
    }
}

Write the index.php test file in the outermost layer. The correct way is to create a demo folder and then write the test file.

test();

So our current file directory structure is as follows. Run index.php to get the content correctly. Form Test -> test()

How to write composer package

You can customize it according to your own To write classes, you only need to pay attention to the fact that the namespace level is the same as the folder level, and the class name is the same as the file name to load automatically.

The above is the detailed content of How to write composer package. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Composer是啥Composer是啥Dec 25, 2023 pm 03:06 PM

Composer是PHP的依赖管理工具,它允许开发者将第三方库和框架与自己的项目进行集成。它的主要功能包括:1、依赖管理;2、版本控制;3、自动加载;4、扩展开发;5、集成其他工具。它简化了PHP项目的依赖管理过程,确保项目的稳定性和可维护性。通过使用Composer,开发者可以更加高效地管理自己的项目和集成第三方库和框架。

四大步教你在Debian11上安装使用Composer!四大步教你在Debian11上安装使用Composer!Nov 08, 2022 pm 04:32 PM

本文由composer​教程栏目给大家介绍关于在Debian11上是怎么一步步安装,以及使用Composer的 ,非常详细哦~希望对需要的朋友有所帮助!

composer 怎么修改php路径composer 怎么修改php路径Oct 24, 2022 am 11:33 AM

composer修改php路径的方法:1、搜索“composer.bat”并复制到项目文件夹;2、编辑“composer.bat”,将内容修改为“@ECHO OFF php "%~dp0composer.phar" %*”即可。

PHP使用Composer安装和管理依赖包PHP使用Composer安装和管理依赖包Jun 18, 2023 pm 03:30 PM

在PHP开发中,我们经常要处理各种依赖包,这些依赖包可能是其他开发者编写的PHP库文件,也可能是一些第三方工具和框架。为了方便管理这些依赖包,我们可以使用Composer来进行相关的安装和管理工作。Composer是一个开源的PHP依赖管理工具,它可以帮助我们自动化安装、更新和卸载PHP依赖包。通过Composer,我们可以轻松地管理我们应用中的不同依赖,同

使用Composer和PHP包管理器的最佳实践使用Composer和PHP包管理器的最佳实践May 23, 2023 am 08:29 AM

随着PHP的日益流行,PHP开发人员面临着许多挑战,其中包括代码管理、可重用性和依赖性管理。这些问题可以使用包管理器来解决,而Composer是PHP最受欢迎的包管理器之一。在本文中,我们将探讨使用Composer和PHP包管理器的最佳实践,从而提高您的PHP开发效率和代码质量。何为Composer?Composer是一款PHP包管理器,它可以轻松管理PHP

创建composer项目的步骤创建composer项目的步骤Feb 19, 2024 pm 07:13 PM

Composer是一个PHP的依赖管理工具,可以帮助开发者有效地管理项目中的依赖关系。通过Composer,我们可以轻松地引入第三方库、框架以及其他项目所需的各种资源。创建一个Composer项目非常简单,只需按照以下步骤进行操作:首先需要确保在本地已经安装了Composer。可以在终端中运行composer-v命令来确认是否已经安装成功。在项目的根目录中

如何在composer上安装和使用如何在composer上安装和使用Feb 19, 2024 pm 09:38 PM

composer是PHP的依赖管理工具,可以方便地安装、更新和管理项目所需的第三方库和依赖。本文将介绍composer的安装与使用,并提供详细的代码示例。一、安装Composer要使用composer,首先需要将其安装到本地开发环境中。以下演示了在Windows系统中安装composer的步骤:打开Composer的官方网站(https://getcompo

composer动画怎么保存composer动画怎么保存Apr 09, 2024 pm 02:02 PM

要保存 Composer 动画,可以使用 Lottie 文件格式,具体步骤为:导出为 JSON 文件;使用 Lottie 工具创建 Lottie 文件;从 Lottie 文件导出为多种格式,包括 JSON、GIF、MP4、SWF、HTML。

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),