A complete developer's guide to modern PHP and the Laravel framework. Learn object-oriented PHP, Eloquent optimization, dependency injection, and Composer package management.
Modern PHP is a fast, type-safe scripting language equipped with an rich ecosystem of tools and frameworks. At the top of this ecosystem sits Laravel, a powerful web framework that values developer ergonomics, clean separation of concerns, and robust package management.
Modern PHP (v8+) supports strict types and clean object-oriented syntax. Placing declare(strict_types=1); at the top of your files forces static compile-time type-safety checks:
<?php
declare(strict_types=1);
namespace App\Services;
interface TaskInterface {
public function execute(string $taskName): bool;
}
class SystemTask implements TaskInterface {
public function __construct(
protected string $environment
) {}
public function execute(string $taskName): bool {
// Run tasks in target environment
return true;
}
}
Laravel is structured as a Model-View-Controller (MVC) framework. It features an IoC (Inversion of Control) container that manages dependency injection:
<?php
// app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use App\Repositories\UserRepository;
use Illuminate\Http\JsonResponse;
class UserController extends Controller {
// UserRepository is automatically resolved and injected
public function __construct(
protected UserRepository $userRepository
) {}
public function index(): JsonResponse {
$users = $this->userRepository->allActive();
return response()->json($users);
}
}
Eloquent provides an ActiveRecord implementation for working with databases. While highly readable, developers must be careful to avoid performance traps like the N+1 query issue.
$books = Book::all(); // 1 query
foreach ($books as $book) {
echo $book->author->name; // N queries (one per book)
}
$books = Book::with('author')->get(); // Only 2 queries
foreach ($books as $book) {
echo $book->author->name;
}
Composer manages third-party libraries inside PHP. The package lifecycle relies on two files:
composer.json: Lists the packages your application declares dependencies on.composer.lock: Records the exact versions of the packages installed, ensuring consistency across environments.To install a package, run:
composer require laravel/sanctum
To install all locked dependencies:
composer install
Modern PHP applications use PHP-FIG coding standards (PSR-12/PSR-4) for automatic namespace class mapping.