Home > Backend Development > PHP Tutorial > PSR-Autoloading Standard in PHP

PSR-Autoloading Standard in PHP

Barbara Streisand
Release: 2025-01-11 16:04:44
Original
433 people have browsed it

PSR-Autoloading Standard in PHP

Ahnii!

Remember PHP's manual require days? Last week, I helped a team upgrade their legacy app – over 50 require statements per file! Let's see how PSR-4 autoloading solves this.

Understanding PSR-4 (5 minutes)

PSR-4 is your code's automatic file locator. Like a GPS using addresses, PSR-4 uses namespaces to find classes.

Key Concepts (2 minutes)

  1. Fully Qualified Class Name (FQCN): VendorPackageClass. Think of it as the complete address of your class.
  2. Directory Structure: A well-organized project directory with namespace-to-directory mappings.

Real-World Example (10 minutes)

Project structure:

<code>vendor/
└── jonesrussell/
    └── blog/
        ├── composer.json
        └── src/
            └── Post/
                ├── PostController.php
                └── PostRepository.php</code>
Copy after login

Setting Up Composer (3 minutes)

composer.json:

{
    "name": "jonesrussell/blog",
    "autoload": {
        "psr-4": {
            "JonesRussell\Blog\": "src/"
        }
    }
}
Copy after login

Creating Classes (2 minutes)

PostController.php:

<?php

namespace JonesRussell\Blog\Post;

class PostController
{
    public function index()
    {
        return ['status' => 'Ready to blog!'];
    }
}
Copy after login

Common Patterns (5 minutes)

Multiple Namespace Roots:

{
    "autoload": {
        "psr-4": {
            "JonesRussell\Blog\": "src/",
            "JonesRussell\Blog\Tests\": "tests/"
        }
    }
}
Copy after login

Nested Namespaces: (File location: src/Core/Database/Connection.php)

<?php

namespace JonesRussell\Blog\Core\Database;

class Connection
{
    private $config;

    public function __construct(array $config)
    {
        $this->config = $config;
    }
}
Copy after login

Framework Examples (5 minutes)

Laravel and Symfony use PSR-4 by default.

Laravel Example:

<?php

namespace App\Http\Controllers;

class BlogController extends Controller
{
    public function index()
    {
        return view('blog.index');
    }
}
Copy after login

Symfony Example:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class BlogController extends AbstractController
{
    public function index(): Response
    {
        return $this->render('blog/index.html.twig');
    }
}
Copy after login

Troubleshooting (3 minutes)

  • "Class Not Found" Errors: Run composer dump-autoload.
  • Directory Structure Issues: Ensure your directory structure matches your namespaces (case-sensitive!).

Testing (2 minutes)

Create test-autoload.php:

<?php

require 'vendor/autoload.php';

$controller = new \JonesRussell\Blog\Post\PostController();
var_dump($controller->index()); // Should output "Ready to blog!"
Copy after login

Next Steps

Next, we'll cover PSR-6 (caching). This is part of our PSR Standards series.

Resources

  • Official PSR-4 Specification
  • Composer Autoloading Documentation
  • Series Example Repository (v0.3.0 - PSR-4 Implementation)

The above is the detailed content of PSR-Autoloading Standard in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template