📲 Download Utility Tools Apps

EMI Calculator

Download EMI Calculator

SOA Games

Download SOA Games

SOA Technology App

Download SOA Technology

BMI Checker

Download BMI Checker

Task Jira

Download Task Jira

Laughing Adda

Download Laughing Adda

📅 हिंदी कैलेंडर ऐप डाउनलोड करें

Download Shubhcalendar App

16. Composer & Packages (PHP)

🔹 What is Composer?

Composer is a dependency manager for PHP.

👉 Simple meaning:
It helps you install, manage, and update external libraries (packages) in your PHP project.

💡 Example:
Instead of writing everything from scratch, you can install ready-made libraries like:

  • Payment gateways
  • Email sending (PHPMailer)
  • PDF generation
  • APIs integration

👉 Composer does this automatically.


🔹 Why Composer is Important?

Without Composer ❌

  • Manually download files
  • Handle dependencies yourself
  • Difficult updates

With Composer ✅

  • One command installs everything
  • Automatically loads files
  • Easy updates

🔹 Installing Composer

Step 1: Download Composer

Go to:
👉 https://getcomposer.org/

Step 2: Install

For Windows:

  • Download Composer-Setup.exe
  • Install normally

Check installation:

composer -v

If version shows → Installed successfully ✅


🔹 Composer Basic Commands

1. Initialize project

composer init

2. Install dependencies

composer install

3. Add a package

composer require monolog/monolog

4. Update packages

composer update

🔹 Installing Libraries (Packages)

👉 Example: Install PHPMailer

composer require phpmailer/phpmailer

Composer will:

  • Download package
  • Create vendor/ folder
  • Update composer.json
  • Generate autoload file

🔹 composer.json File

This is the main file for Composer.

Example:

{
"require": {
"phpmailer/phpmailer": "^6.0"
}
}

👉 It stores:

  • Project dependencies
  • Version control

🔹 Autoloading (Very Important)

Composer provides automatic class loading.

👉 Instead of:

require 'file1.php';
require 'file2.php';
require 'file3.php';

👉 Use:

require 'vendor/autoload.php';

Now all classes are automatically loaded ✅


🔹 Example Usage

<?phprequire 'vendor/autoload.php';use PHPMailer\PHPMailer\PHPMailer;$mail = new PHPMailer();
$mail->setFrom('test@example.com', 'Test');
$mail->addAddress('user@example.com');
$mail->Subject = 'Hello';
$mail->Body = 'This is a test email';$mail->send();

🔹 PSR-4 Autoloading (Advanced)

You can define your own classes:

{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}

Then run:

composer dump-autoload

Now you can use:

use App\Controllers\UserController;

🔹 Folder Structure After Composer

project/

├── composer.json
├── composer.lock
├── vendor/
│ ├── autoload.php
│ └── packages...

🔥 Interview Questions

  • What is Composer in PHP?
  • Difference between require and require_once vs Composer autoload?
  • What is composer.json?
  • What is PSR-4?
  • Difference between install and update?


AI Spiritual Tools & Interactive Experiences

Explore powerful AI-driven tools for daily guidance, spirituality, fun quizzes, and self-discovery.

Today’s Quote

Get inspiring daily quotes powered by AI to motivate and guide your day.

Explore Now

AI Tarot Card Reader

Reveal insights about your future, love, and career with AI tarot readings.

Read Tarot

Love Match Calculator

Check compatibility and love predictions using AI-based analysis.

Check Match

Fortune Cookie

Open an AI fortune cookie and receive wisdom, luck, and fun messages.

Open Cookie

Quiz Categories

Engage with knowledge-based and fun quizzes across multiple categories.

Start Quiz

Panchang Calendar

View daily Panchang, auspicious timings, tithi, nakshatra, and festivals.

View Panchang

Online Numerology

Discover your destiny number, life path, and numerology predictions.

Calculate Now

Spiritual Feeds

Stay connected with spiritual thoughts, mantras, and divine content.

View Feeds

Quiz Hub

Attempt trending quizzes on GK, spirituality, festivals, and more.

Explore Quizzes