← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 7 of 24

PHP Composer और Packages

Composer PHP code के लिए एक app store जैसा है। आप जो helper libraries चाहते हैं उन्हें list करते हैं, और यह उन्हें download करके आपके लिए up to date रखता है।
Syntax
php
// composer.json
{
    "require": {
        "vendor/package": "^1.0"
    }
}

// composer install
require "vendor/autoload.php";

Composer क्या है?

Composer PHP का standard package manager है, आपको यह declare करने देते हुए कि आपके project के third-party libraries पर कौन सी dependencies हैं और सही versions को automatically download करते हुए बजाय code हाथ से copy करने के।

उदाहरण: What is Composer?

php
<?php
// Print "Composer downloads the exact dependency versions your project declares." to the output
echo "Composer downloads the exact dependency versions your project declares.";
?>

composer.json बनाना

composer.json आपके project root में बैठता है और हर dependency को एक version constraint के साथ list करता है, इस बात का single source of truth बनकर कि आपके project को चलने के लिए क्या चाहिए।

उदाहरण: Creating composer.json

php
<?php
$composerJson = [
    "require" => [
        "monolog/monolog" => "^2.0"
    ]
];
// Print `json_encode($composerJson, JSON_PRETTY_PRINT)` to the output
echo json_encode($composerJson, JSON_PRETTY_PRINT);
?>

Packages Install करना

composer install चलाना composer.json पढ़ता है, हर dependency के लिए compatible versions resolve करता है, और उन सबको एक vendor/ folder में download कर देता है जहाँ से आपका code फिर require करता है।

उदाहरण: Installing Packages

php
<?php
// Run in terminal: composer install
echo "Downloads dependencies into vendor/ based on composer.json";
?>

Composer का Autoloader Load करना

अपनी entry script के top पर vendor/autoload.php include करना Composer का autoloader wire up कर देता है, जिसका मतलब है हर installed package की classes बिना एक भी manual require statement के available हो जाती हैं।

उदाहरण: Loading Composer's Autoloader

php
<?php
// require __DIR__ . '/vendor/autoload.php';
echo "One require statement makes every installed package's classes available";
?>

Package Versions Manage करना

Composer semantic versioning conventions follow करता है: एक caret (^1.2.0) कोई भी non-breaking update allow करता है, जबकि एक tilde (~1.2.0) updates को सिर्फ patch-level releases तक restrict करता है, आपको यह fine control देते हुए कि dependencies कितने aggressively update हों।

उदाहरण: Managing Package Versions

php
<?php
$constraints = [
    "^1.2.0" => "any non-breaking update",
    "~1.2.0" => "patch-level updates only",
];
// Loop over `$constraints`, binding each item to `$range => $meaning`
foreach ($constraints as $range => $meaning) {
    // Print "$range: $meaning\n" to the output
    echo "$range: $meaning\n";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. require 'vendor/autoload.php'; भूल जाना, ताकि installed packages न मिल सकें।
  2. composer.json और composer.lock files के बजाय vendor folder को version control में commit करना।
  3. composer.lock को हाथ से edit करना, जो version mismatches का कारण बन सकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.