DEV Community

Russell Jones
Russell Jones

Posted on • Originally published at jonesrussell.github.io on

PSR-4: Autoloading Standard in PHP

Ahnii!

Prerequisites: Basic PHP OOP, Composer installed. Recommended: Read PSR-1 first.

Remember the old days of PHP when you had to manually require every single file? Imagine modernizing a legacy application with 50+ require statements at the top of each file. PSR-4 autoloading makes this a problem of the past.

Understanding PSR-4

Think of PSR-4 as your code's GPS system - it helps PHP find the right files automatically. Just like how a GPS uses addresses to find locations, PSR-4 uses namespaces to locate classes.

Key Concepts

  1. Fully Qualified Class Name (FQCN)

    • Vendor namespace (like your brand)
    • Package namespace (like your project)
    • Class name (the actual file)
  2. Directory Structure

    • Base directory (where everything starts)
    • Namespace mapping (your GPS coordinates)
    • File location rules (the actual addresses)

Real-World Example

Here's a well-structured project layout:

vendor/
└── jonesrussell/
    └── blog/
        ├── composer.json
        └── src/
            └── Post/
                ├── PostController.php
                └── PostRepository.php
Enter fullscreen mode Exit fullscreen mode

1. Setting Up Composer

{
    "name": "jonesrussell/blog",
    "autoload": {
        "psr-4": {
            "JonesRussell\\Blog\\": "src/"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Creating Classes

<?php

namespace JonesRussell\Blog\Post;

class PostController
{
    public function index()
    {
        return ['status' => 'Ready to blog!'];
    }
}
Enter fullscreen mode Exit fullscreen mode

Common Patterns

1. Multiple Namespace Roots

{
    "autoload": {
        "psr-4": {
            "JonesRussell\\Blog\\": "src/",
            "JonesRussell\\Blog\\Tests\\": "tests/"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Nested Namespaces

<?php

namespace JonesRussell\Blog\Core\Database;

class Connection
{
    private $config;

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

// File location: src/Core/Database/Connection.php
Enter fullscreen mode Exit fullscreen mode

Framework Examples

If you're using Laravel or Symfony, they follow PSR-4 out of the box:

Laravel

<?php

namespace App\Http\Controllers;

class BlogController extends Controller
{
    public function index()
    {
        return view('blog.index');
    }
}
Enter fullscreen mode Exit fullscreen mode

Symfony

<?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');
    }
}
Enter fullscreen mode Exit fullscreen mode

Quick Fixes for Common Issues

  1. "Class Not Found" Errors
# When things go wrong, this is your friend:
composer dump-autoload
Enter fullscreen mode Exit fullscreen mode
  1. Directory Structure Mistakes
# Don't do this
src/
└── controllers/  # lowercase = bad
    └── PostController.php

# Do this instead
src/
└── Controller/  # Matches namespace case
    └── PostController.php
Enter fullscreen mode Exit fullscreen mode

Testing Your Setup

Drop this in test-autoload.php:

<?php

require 'vendor/autoload.php';

// If this works, your autoloading is set up correctly!
$controller = new \JonesRussell\Blog\Post\PostController();
var_dump($controller->index()); // Should show "Ready to blog!"
Enter fullscreen mode Exit fullscreen mode

Next Steps

Tomorrow, you'll explore PSR-6 and see how it standardizes caching in PHP applications. This post is part of the PSR Standards in PHP series.

Resources

Baamaapii

Top comments (0)