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

PSR-Autoloading Standard in PHP

Barbara Streisand
Barbara StreisandOriginal
2025-01-11 16:04:44360browse

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>

Setting Up Composer (3 minutes)

composer.json:

<code class="language-json">{
    "name": "jonesrussell/blog",
    "autoload": {
        "psr-4": {
            "JonesRussell\Blog\": "src/"
        }
    }
}</code>

Creating Classes (2 minutes)

PostController.php:

<code class="language-php"><?php

namespace JonesRussell\Blog\Post;

class PostController
{
    public function index()
    {
        return ['status' => 'Ready to blog!'];
    }
}</code>

Common Patterns (5 minutes)

Multiple Namespace Roots:

<code class="language-json">{
    "autoload": {
        "psr-4": {
            "JonesRussell\Blog\": "src/",
            "JonesRussell\Blog\Tests\": "tests/"
        }
    }
}</code>

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

<code class="language-php"><?php

namespace JonesRussell\Blog\Core\Database;

class Connection
{
    private $config;

    public function __construct(array $config)
    {
        $this->config = $config;
    }
}</code>

Framework Examples (5 minutes)

Laravel and Symfony use PSR-4 by default.

Laravel Example:

<code class="language-php"><?php

namespace App\Http\Controllers;

class BlogController extends Controller
{
    public function index()
    {
        return view('blog.index');
    }
}</code>

Symfony Example:

<code class="language-php"><?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');
    }
}</code>

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:

<code class="language-php"><?php

require 'vendor/autoload.php';

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

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:
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