-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
250: Common PHP Interview Questions on Composer and Autoloading
If you're heading into a PHP interview, you can bet your bottom dollar that Composer and autoloading will come up. Interviewers love these topics because they separate the people who just "copy-paste from a tutorial" from the engineers who actually understand how the PHP ecosystem fits together. I've sat on both sides of the table, and the questions usually center around the why, not just the how.
What is the actual difference between composer install and composer update?
This is the classic "gotcha" question. If you say "they both install packages," you've failed. The difference is all about the composer.lock file.
When you run composer install, Composer looks for a composer.lock file. If it exists, it installs the exact versions listed there. It doesn't matter if a newer version of a library is available; it wants your environment to be identical to everyone else's on the team. If there is no lock file, it behaves like an update.
composer update, on the other hand, ignores the lock file. It looks at your composer.json, finds the latest versions that fit your version constraints (like ^2.0), installs them, and then updates the composer.lock file to reflect those new versions. I always tell my juniors: never run update on a production server. You're essentially rolling the dice on whether a dependency update will break your site in the middle of the day.
How does PSR-4 autoloading actually work under the hood?
Interviewers want to know if you understand that "magic" isn't actually magic—it's just a mapping system. PSR-4 is a standard that maps a namespace prefix to a specific directory.
Let's say you have this in your composer.json:
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
When you try to instantiate a class like new App\Services\PaymentGateway(), PHP realizes it doesn't know where that class is. Instead of crashing, it asks the registered autoloader (Composer's). Composer looks at that map and says, "Okay, App\ means the src/ folder." It then converts the rest of the namespace Services\PaymentGateway into a file path: src/Services/PaymentGateway.php. If the file exists, it's included. If not, it throws a Fatal Error.
Why do we have to include vendor/autoload.php in every entry point?
I've seen a lot of developers just do this because "that's how it's done," but the technical reason is the SPL (Standard PHP Library) autoloader. PHP has a built-in function called spl_autoload_register() that allows you to define a custom function to be called whenever a class is not found.
The vendor/autoload.php file is essentially a wrapper that calls spl_autoload_register() and passes in Composer's complex logic for finding files. If you forget to include it, PHP has no instructions on where to find your classes or your dependencies. You'd be forced to go back to the dark ages of manually writing require_once 'src/Services/PaymentGateway.php'; at the top of every single file, which is a maintenance nightmare in any project larger than a few scripts.
What happens if I change a namespace or move a file?
This is where people get stuck. If you move src/Services/PaymentGateway.php to src/Infrastructure/Payments/PaymentGateway.php and update the namespace inside the file, you might find that PHP still can't find it—or worse, it's acting weird.
While PSR-4 is dynamic, Composer often optimizes the autoloader for production using composer dump-autoload -o. This creates a static "class map" (a giant associative array) to avoid hitting the file system repeatedly. If you've optimized your autoloader, any move or name change requires you to run composer dump-autoload again to refresh that map. I've spent hours debugging "Class not found" errors only to realize I forgot to refresh the class map after a refactor.
📋 Practical Task
Exercise: Fixing a Broken PSR-4 Namespace Mapping
You have inherited a legacy project where a developer tried to reorganize the folders but didn't update the configuration correctly. The application is throwing a Fatal error: Class "App\Billing\InvoiceGenerator" not found.
Current Project Structure:
/project
/src
/Billing
InvoiceGenerator.php
composer.json
index.php
The contents of composer.json:
{
"autoload": {
"psr-4": {
"Project\\": "src/"
}
}
}
The contents of src/Billing/InvoiceGenerator.php:
<?php
namespace App\Billing;
class InvoiceGenerator {
public function generate() {
return "Invoice generated!";
}
}
The contents of index.php:
<?php
require 'vendor/autoload.php';
$invoice = new App\Billing\InvoiceGenerator();
echo $invoice->generate();
Your Task: Identify the mismatch between the composer.json mapping and the actual namespace used in the class file. Fix the composer.json file so that the App\ namespace correctly maps to the src/ directory, and explain which Composer command you must run in the terminal to apply this change to the autoloader.
There are no comments for now.