Composer for PHP Flashcards

1
Q

Q: What is Composer in PHP?

A

A: A dependency manager for PHP projects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: How do you install Composer globally?

A

php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”
php composer-setup.php
mv composer.phar /usr/local/bin/composer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: How do you verify Composer is installed?

A

A: Run composer –version.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: What is the default file used by Composer to manage dependencies?

A

A: composer.json.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: What command creates a new composer.json file?

A

A: composer init.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: How do you install a package with Composer?

A

A: composer require vendor/package-name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: What command installs all dependencies listed in composer.json?

A

A: composer install.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: How do you update all dependencies to their latest versions?

A

A: composer update.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: How do you install a specific version of a package?

A

A: composer require vendor/package-name:^1.2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: How do you install dependencies without autoloading?

A

A: composer install –no-autoloader.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: What is the purpose of the require section in composer.json?

A

A: Specifies the project’s dependencies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: What is the require-dev section used for?

A

A: Lists dependencies needed only for development.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: How do you specify a PHP version requirement in composer.json?

A

“require”: {
“php”: “>=7.4”
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: What is the autoload section in composer.json for?

A

A: Defines how classes and files are loaded automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: What is the scripts section in composer.json?

A

A: Defines custom scripts that can be executed with Composer commands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: What is Composer’s autoloader file?

A

A: vendor/autoload.php.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q: How do you include Composer’s autoloader in your project?

A

A: require ‘vendor/autoload.php’;.

18
Q

Q: What is PSR-4 autoloading in Composer?

A

A: A standard for autoloading classes using namespaces and directory structure.

19
Q

Q: How do you define PSR-4 autoloading in composer.json?

A

“autoload”: {
“psr-4”: {
“Namespace\”: “src/”
}
}

20
Q

Q: How do you regenerate the autoloader?

A

A: composer dump-autoload.

21
Q

Q: How do you show installed packages?

A

A: composer show.

22
Q

Q: How do you show details about a specific package?

A

A: composer show vendor/package-name.

23
Q

Q: How do you remove a package?

A

A: composer remove vendor/package-name.

24
Q

Q: How do you check for outdated dependencies?

A

A: composer outdated.

25
Q

Q: How do you list globally installed Composer packages?

A

A: composer global show.

26
Q

Q: What is the purpose of composer.lock?

A

A: Locks dependency versions to ensure consistency across environments.

27
Q

Q: Should you commit composer.lock to version control?

A

A: Yes, to ensure consistent dependency versions for all developers.

28
Q

Q: How do you install dependencies exactly as listed in composer.lock?

A

A: composer install.

29
Q

Q: What command updates the composer.lock file?

A

A: composer update.

30
Q

Q: How do you check for changes in composer.lock?

A

A: Use version control tools like git diff.

31
Q

Q: How do you install only production dependencies?

A

A: composer install –no-dev.

32
Q

Q: How do you install only development dependencies?

A

A: Use composer require –dev.

33
Q

Q: How do you run a custom script defined in composer.json?

A

A: composer run-script script-name.

34
Q

Q: How do you check the current environment in Composer?

A

A: Use the COMPOSER_DEV_MODE environment variable.

35
Q

Q: How do you validate the syntax of composer.json?

A

A: composer validate.

36
Q

Q: What is the config command used for in Composer?

A

A: To set or get configuration options, e.g., composer config.

37
Q

Q: How do you create a global Composer package?

A

A: Install it globally with composer global require vendor/package-name.

38
Q

Q: What does the prefer-dist flag do during installation?

A

A: Downloads pre-built distribution archives instead of cloning repositories.

39
Q

Q: How do you create a Composer package?

A

A: Add a composer.json file to the project and publish it to Packagist.

40
Q

Q: How do you specify a package repository other than Packagist?

A

A: Add a repositories section in composer.json.