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
37: Overview of Laravel and Symfony
Up until now, we've been writing "plain" PHP. You've handled your own routing, managed your own database connections, and structured your folders however you liked. But in the professional world, you'll rarely start from a blank slate. You'll likely use a framework. In the PHP ecosystem, that almost always means choosing between Laravel and Symfony.
Before we dive into the differences, let me show you a mistake I see almost every single developer make when they first transition from vanilla PHP to a modern framework. They try to treat the framework like a library rather than an ecosystem.
The "Too Few Arguments" Crash
// A simplified Controller in a framework like Symfony or Laravel
class UserController {
private $userRepository;
public function __construct(UserRepository $userRepository) {
$this->userRepository = $userRepository;
}
public function show($id) {
return $this->userRepository->find($id);
}
}
// The "Vanilla" mistake:
$controller = new UserController();
echo $controller->show(1);
If you run this, PHP will scream at you: Fatal error: Uncaught ArgumentCountError: Too few arguments to function UserController::__construct(), 0 passed.... When you're used to vanilla PHP, your instinct is to instantiate the class you need using new. But in Laravel and Symfony, you almost never manually instantiate a controller or a service.
Letting the Service Container Handle the Heavy Lifting
The fix isn't to manually create a UserRepository and pass it in. The fix is to stop using the new keyword for your core logic classes and let the framework's Service Container do it for you.
In both Laravel and Symfony, you define a route that points to a controller method. When a request hits that route, the framework looks at the constructor of your controller, sees that it needs a UserRepository, automatically creates that repository, and "injects" it into the controller for you. This is called Dependency Injection.
To fix the code above, you simply define the route in the framework's routing file (like web.php in Laravel or routes.yaml in Symfony) and let the framework call the method. You don't call the controller yourself. By delegating the object creation to the framework, you can swap out the UserRepository for a mock version during testing without ever changing a line of code in your controller.
Rapid Prototyping vs. Enterprise Modularism
So, which one should you actually use? It's the age-old PHP debate. I like to think of it as a choice between "magic" and "explicitly."
Laravel is designed for developer happiness. It's incredibly fast to get an app off the ground. It uses "Facades" and "Magic Methods" that allow you to do things like User::all() without having to explicitly instantiate a database manager. It feels seamless, and the ecosystem (Forge, Vapor, Nova) is top-tier. The downside? Sometimes the "magic" makes it harder to debug when something goes wrong deep in the framework's internals.
Symfony is the "grown-up" in the room. It's modular, strict, and incredibly stable. In Symfony, you'll find yourself writing more configuration and being more explicit about how your services are wired together. It's less "magic" and more "engineering." This makes it the gold standard for massive, enterprise-level applications where predictability is more important than development speed.
The Symfony Components Secret
Here is the secret that usually surprises people: Laravel isn't actually a competitor to Symfony in the way you think. Laravel is actually built on top of many Symfony components.
- Laravel's HTTP Foundation? That's Symfony.
- Laravel's Routing system? Heavily inspired by and utilizing Symfony components.
- Laravel's Console component? Also Symfony.
When you choose Laravel, you're getting a polished, opinionated layer of "sugar" wrapped around the rock-solid, modular components that Symfony provides. If you want a framework that tells you the "best" way to do things and lets you move fast, go Laravel. If you want to build a custom architecture where you pick and choose every single component, go Symfony.
📋 Practical Task
Exercise: Implementing Dependency Injection in a Mock Framework
You are tasked with simulating how a Service Container works. You have a DatabaseConnection class and a UserMapper class that depends on that connection. Instead of manually instantiating them, you must create a simple Container class that can "resolve" these dependencies.
Requirements:
- Create a
DatabaseConnectionclass with a methodconnect()that returns "Connected to DB". - Create a
UserMapperclass that requiresDatabaseConnectionin its constructor. It should have a methodgetUser()that returns "User data from [Connection Status]". - Create a
Containerclass with a methodget($className). This method should use PHP'sReflectionClassto check if the class has a constructor, automatically instantiate any required dependencies, and return the final object. - Test your container by calling
$container->get(UserMapper::class)and verifying that theUserMapperwas created with its dependency already injected.
There are no comments for now.