PHP
Completed
-
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
40: Dependency Management with Composer
I want to show you a snippet of code that I've seen countless developers struggle with when they first move from "vanilla" PHP to professional projects. Imagine you've decided to use the popular Carbon library to handle some complex date math. You've run the install command in your terminal, and you're feeling good. But then you run this:
<?php
use Carbon\Carbon;
$date = Carbon::now()->addDays(5);
echo "Your subscription expires on: " . $date->toDateString();
?>
You hit refresh in your browser and get a screen-filling error: Fatal error: Uncaught Error: Class "Carbon\Carbon" not found. You check your folder, and the vendor directory is right there. The files exist. You might even try to fix it by manually including the file with require 'vendor/nesbot/carbon/src/Carbon.php';, only to get another error because Carbon depends on other classes that aren't loaded yet.
The missing bridge: vendor/autoload.php
The problem here isn't that the library isn't installed; it's that PHP has no idea where to find those classes when you call them. In the old days, we used to have massive lists of require and include statements at the top of every file. It was a maintenance nightmare. If you moved a file or added a new library, everything broke.
Composer solves this with an autoloader. Instead of you telling PHP where every single class is, Composer creates a map of the entire vendor directory. You only need to require one file, and Composer handles the rest on the fly.
<?php
require 'vendor/autoload.php'; // This is the magic line
use Carbon\Carbon;
$date = Carbon::now()->addDays(5);
echo "Your subscription expires on: " . $date->toDateString();
?>
Now, when you call Carbon::now(), PHP asks the Composer autoloader, "Hey, do you know where the Carbon class is?" Composer looks at its map and loads the file for you instantly. I can't stress this enough: if you don't have require 'vendor/autoload.php'; at your entry point, nothing you install via Composer will work.
Defining your needs in composer.json
When you run composer require nesbot/carbon, Composer doesn't just download files; it updates a file called composer.json. Think of this as the "manifest" for your project. It tells anyone else who downloads your code exactly which libraries are required to make the app run.
I highly recommend opening this file and looking at it. You'll see a "require" section. If you're working in a team, you don't commit the vendor folder to Git (it's too huge and redundant). Instead, you commit the composer.json. When your teammate clones the repo, they just run composer install, and Composer reads that manifest to rebuild the vendor folder on their machine.
Why the .lock file is your best friend in production
You'll also notice a composer.lock file. This often confuses people—they think it's a temporary file. It's not. It is arguably the most important file for stability.
The composer.json file usually allows for versions (e.g., ^2.0, which means "anything from 2.0 up to, but not including, 3.0"). If you only had the JSON file, you might install version 2.1 today, but your server might install 2.5 next week. If 2.5 introduced a breaking change, your site would crash in production while working perfectly on your laptop.
The composer.lock file records the exact version and commit hash of every package installed. When you run composer install, Composer checks the lock file first. This ensures that every single environment—your laptop, your colleague's Mac, and your Linux production server—is running the exact same byte-for-byte code.
📋 Practical Task
Building a File-Based Audit Log with Monolog
In this exercise, you will integrate a professional logging library into a project. Instead of using error_log() or echo to track events, you'll use Monolog, the industry standard for PHP.
Your Goal: Create a script that logs a "User Login" event to a local file called app.log.
- Open your terminal in your project directory and install Monolog by running:
composer require monolog/monolog. - Create a file named
logger.php. - Inside
logger.php:- Include the Composer autoloader.
- Import the
LoggerandStreamHandlerclasses from theMonolognamespace. - Create a new Logger instance named "auth_system".
- Push a
StreamHandlerto the logger that points to a file namedapp.logand sets the minimum logging level toLogger::INFO. - Trigger a log entry using
$log->info('User "Admin" successfully logged in.');
- Run the script via CLI (
php logger.php) and verify thatapp.logwas created and contains the log entry with a timestamp.
There are no comments for now.